#include #include #include #include using namespace std; struct Cell { int X, Y, Life; Cell(int x, int y, int life) { X = x; Y = y; Life = life; } }; struct gret { bool operator ()(const Cell &left, const Cell &right) { return left.Life < right.Life; } }; int N, V, Ox, Oy; int L[250][250]; int Next[4] = { 1,0,-1,0 }; int main() { cin >> N >> V >> Ox >> Oy; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> L[i][j]; } } priority_queue,gret>pq; pq.push(Cell(1, 1, V)); bool B[250][250]; int oLife = -1; while (pq.size() > 0) { Cell c = pq.top(); pq.pop(); B[c.Y - 1][c.X - 1] = true; if (c.Y == N&&c.X == N) { cout << "YES" << endl; return 0; } if (c.Y == Oy&&c.X == Ox) { oLife = c.Life; } for (int i = 0; i < 4; i++) { int nextY = c.Y + Next[i]; int nextX = c.X + Next[(i + 1) % 4]; if (nextX < 1 || nextY < 1) continue; if (nextX > N || nextY > N) continue; if (B[nextY - 1][nextX - 1]) continue; int nextLife = c.Life - L[nextY - 1][nextX - 1]; if (nextLife <= 0) continue; pq.push(Cell(nextX, nextY, nextLife)); } } if (Ox == 0 && Oy == 0) { cout << "NO" << endl; return 0; } pq = priority_queue,gret>(); pq.push(Cell(Ox, Oy, oLife * 2)); for (int i = 0; i < 250; i++) { for (int j = 0; j < 250; j++) { B[i][j] = false; } } while (pq.size() > 0) { Cell c = pq.top(); pq.pop(); B[c.Y - 1][c.X - 1] = true; if (c.Y == N&&c.X == N) { cout << "YES" << endl; return 0; } for (int i = 0; i < 4; i++) { int nextY = c.Y + Next[i]; int nextX = c.X + Next[(i + 1) % 4]; if (nextX < 1 || nextY < 1) continue; if (nextX > N || nextY > N) continue; if (B[nextY - 1][nextX - 1]) continue; int nextLife = c.Life - L[nextY - 1][nextX - 1]; if (nextLife <= 0) continue; pq.push(Cell(nextX, nextY, nextLife)); } } cout << "NO" << endl; return 0; }