#include #include #include #include #include #include using namespace std; const int INF = 1e9; vector< vector > dessert; vector< vector > hp; int n; void show(vector< vector > &vec) { int n = vec.size(); for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { printf("%3d ", vec[i][j]); } printf("\n"); } } bool walk(int sx, int sy) { queue< pair > que; que.push(make_pair(sx, sy)); int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int x, y, nx, ny; while (!que.empty()) { auto now = que.front(); x = now.first; y = now.second; que.pop(); for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (hp[y][x] > dessert[ny][nx] && hp[y][x] - dessert[ny][nx] > hp[ny][nx]) { hp[ny][nx] = hp[y][x] - dessert[ny][nx]; if (nx == n && ny == n) { return true; } que.push(make_pair(nx, ny)); } } } return false; } int main() { int v, ox, oy; cin >> n >> v >> ox >> oy; dessert.assign(n + 2, vector(n + 2, INF)); hp.assign(n + 2, vector(n + 2, -1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> dessert[i][j]; } } hp[1][1] = v; bool isGoal = walk(1, 1); // printf("%s\n", isGoal ? "OK" : "NG"); // show(hp); // cout << endl; if (!isGoal) { int hp_oasys = hp[oy][ox]; // printf("%d\n", hp_oasys); if (hp_oasys <= 0) { isGoal = false; } else { hp.assign(n + 2, vector(n + 2, -1)); hp[oy][ox] = 2 * hp_oasys; isGoal = walk(ox, oy); } // show(hp); } cout << (isGoal ? "YES" : "NO") << endl; return 0; }