#include #include #include #define MAXN 1000 using namespace std; int maze[MAXN][MAXN]; int visited[MAXN][MAXN]; int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; struct pos{ int x, y; void init(int X, int Y){ x=X; y=Y; } void print(){ printf("%d %d\n", x,y); } }; int N; pos start; inline bool is_ok(int x, int y){ if(x < 1 || x > N){return false;} if(y < 1 || y > N){return false;} if(visited[y][x] > 0){return false;} if(x == start.x && y == start.y){return false;} return true; } int main() { freopen ("ice.in", "r", stdin); freopen ("ice.out", "w" , stdout); scanf("%d", &N); int a; for(int y=1; y<=N; y++){ for(int x=1; x<=N; x++){ scanf("%d", &a); maze[y][x] = a; visited[y][x] = 0; //printf("%d ", maze[y][x]); } //printf("\n"); } int sx, sy, ex, ey; scanf("%d", &sx); scanf("%d", &sy); scanf("%d", &ex); scanf("%d", &ey); start.init(sx, sy); queue Q; Q.push(start); bool jump; int x, y; while ( !Q.empty() ){ pos cur = Q.front(); Q.pop(); for(int i = 0 ; i < 4; i++){ x = cur.x + dirs[i][1]; y = cur.y + dirs[i][0]; if(!is_ok(x,y)){continue;} jump = true; while (maze[y][x] == 1){ x = x + dirs[i][1]; y = y + dirs[i][0]; if(!is_ok(x,y)){ jump = false; break; } } if(!jump){continue;} visited[y][x] = visited[cur.y][cur.x] + 1; //printf("%d %d\n", x, y); pos next; next.init(x, y); Q.push(next); } } /* for(int y=1; y<=N; y++){ for(int x=1; x<=N; x++){ printf("%d ", visited[y][x]); } printf("\n"); } */ int ans = (visited[ey][ex] > 0) ? visited[ey][ex] : -1; printf("%d\n", ans); return 0; }