#include #include #include #include #include #include #include using namespace std; typedef signed long long ll; #define FOR(x,to) for(x=0;x>N>>V>>SX>>SY>>GX>>GY; FOR(y,N) { FOR(x,N) { cin>>Cost[y][x]; } } SX--,SY--,GX--,GY--; ZERO(HP[0]); HP[0][SY][SX] = V; int dx[] = {-1,0,1,0}; int dy[] = {0,-1,0,1}; for(int i = 0; i < 10001;i++) { int current = i % 2; int next = (i+1) % 2; ZERO(HP[next]); FOR(y, N) { FOR(x, N) { if(HP[current][y][x] > 0) { for(int k = 0;k < 4;k++) { int nextX = x + dx[k]; int nextY = y + dy[k]; if(nextX < 0 || nextY < 0 || nextX >=N || nextY >= N) { continue; } int nextHP = HP[current][y][x] - Cost[nextY][nextX]; if(nextHP <= 0) { continue; } HP[next][nextY][nextX] = max(HP[next][nextY][nextX], nextHP); } } } } if(HP[next][GY][GX] > 0) { printf("%d\n", i+1); return; } } printf("-1\n"); return; } int main() { solve(); return 0; }