#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef unsigned long long uint64; typedef long long int64; using namespace std; //int main() //{ // int N, K, R, B, L,cur, start = 0, res = 0; // freopen("board.in", "r", stdin); // scanf("%d %d", &N, &K); // scanf("%d", &R); // for(int i = 0 ; i < R; ++i) // { // scanf("%d %d", &B, &L); // cur = B - start; // res += cur / K; // start = B + L; // } // res += (N - start) / K; // fclose(stdin); // freopen("board.out","w", stdout); // printf("%d\n", res); // fclose(stdout); // // // return 0; //} //int main() //{ // int N, M, C, P, res = 0; // vector Ivancho, magazin; // freopen("toys.in", "r", stdin); // scanf("%d %d", &N, &M); // for(int i = 0; i < N; ++i) // { // scanf("%d", &C); // Ivancho.push_back(C); // } // sort(Ivancho.begin(), Ivancho.end()); // for(int i = 0; i < M; ++i) // { // scanf("%d", &P); // magazin.push_back(P); // } // sort(magazin.begin(), magazin.end()); // int szIv = Ivancho.size(); // int sz = magazin.size(); // for(int i = 0; i < N; ++i) // { // res += Ivancho[--szIv] *magazin[--sz] ; // } // fclose(stdin); // freopen("toys.out", "w", stdout); // printf("%d\n", res); // fclose(stdout); // // // return 0; //} bool mass[1002][1002]; bool chek [1002][1002]; queue, int> > Q; bool isIce(int x, int y) { return mass[x][y]; } bool can_move(int x, int y, int sz) { if(x <= sz && y <= sz) { return !chek[x][y]; } return false; } int BFS(int strX, int strY,int endX,int endY, int sz) { pair cur = make_pair(strX, strY); chek[cur.first][cur.second] = true; Q.push(make_pair(cur, 0)); while(!Q.empty()) { pair, int> cur = Q.front(); Q.pop(); if(can_move(cur.first.first + 1, cur.first.second, sz)) { if(isIce(cur.first.first + 1, cur.first.second)) { int offset = 1; while(1) { if(!isIce(cur.first.first + offset, cur.first.second)) { if(cur.first.first + offset <= sz) { if(cur.first.first + offset != endX || cur.first.second != endY) { Q.push(make_pair(make_pair(cur.first.first + offset, cur.first.second), cur.second + 1)); chek[cur.first.first + offset][cur.first.second] = true; } else { return cur.second + 1; } } break; } else { ++offset; } } } else { if(cur.first.first + 1 != endX || cur.first.second != endY) { Q.push(make_pair(make_pair(cur.first.first + 1, cur.first.second), cur.second + 1)); chek[cur.first.first + 1][cur.first.second] = true; } else { return cur.second + 1; } } } if(can_move(cur.first.first - 1, cur.first.second , sz)) { if(isIce(cur.first.first - 1, cur.first.second)) { int offset = 1; while(1) { if(!isIce(cur.first.first - offset, cur.first.second)) { if(cur.first.first - offset <= 0) { if(cur.first.first - offset != endX || cur.first.second != endY) { Q.push(make_pair(make_pair(cur.first.first - offset, cur.first.second), cur.second + 1)); chek[cur.first.first - offset][cur.first.second] = true; } else { return cur.second + 1; } } break; } else { ++offset; } } } else { if(cur.first.first - 1 != endX || cur.first.second != endY) { Q.push(make_pair(make_pair(cur.first.first - 1, cur.first.second), cur.second + 1)); chek[cur.first.first - 1][cur.first.second] = true; } else { return cur.second + 1; } } } if(can_move(cur.first.first , cur.first.second + 1, sz)) { if(isIce(cur.first.first , cur.first.second + 1)) { int offset = 1; while(1) { if(!isIce(cur.first.first , cur.first.second + offset)) { if(cur.first.second + offset <= sz) { if(cur.first.first != endX || cur.first.second + offset != endY) { Q.push(make_pair(make_pair(cur.first.first, cur.first.second + offset), cur.second + 1)); chek[cur.first.first][cur.first.second + offset] = true; } else { return cur.second + 1; } } break; } else { ++offset; } } } else { if(cur.first.second + 1 <= sz) { if(cur.first.first != endX || cur.first.second + 1 != endY) { Q.push(make_pair(make_pair(cur.first.first, cur.first.second + 1), cur.second + 1)); chek[cur.first.first][cur.first.second + 1] = true; } else { return cur.second + 1; } } } } if(can_move(cur.first.first , cur.first.second - 1, sz)) { if(isIce(cur.first.first , cur.first.second - 1)) { int offset = 1; while(1) { if(!isIce(cur.first.first , cur.first.second - offset)) { if(cur.first.second - offset >= 0) { if(cur.first.first != endX || cur.first.second - offset != endY) { Q.push(make_pair(make_pair(cur.first.first, cur.first.second - offset), cur.second + 1)); chek[cur.first.first][cur.first.second - offset] = true; } else { return cur.second + 1; } Q.push(make_pair(make_pair(cur.first.first , cur.first.second - offset), cur.second + 1)); } break; } else { ++offset; } } } else { if(cur.first.second - 1 >= 0) { if(cur.first.first != endX || cur.first.second - 1 != endY) { Q.push(make_pair(make_pair(cur.first.first, cur.first.second - 1), cur.second + 1)); chek[cur.first.first][cur.first.second - 1] = true; } else { return cur.second + 1; } Q.push(make_pair(make_pair(cur.first.first , cur.first.second - 1), cur.second + 1)); } } } } return -1; } int main() { int N, x, y, x2, y2; freopen("toys.in", "r", stdin); scanf("%d", &N); for(int line = 1; line <= N; ++line) { for(int col = 1; col <= N; ++col) { scanf("%d", &x); mass[line][col] = x; } } scanf("%d %d", &x, &y); scanf("%d %d", &x2, &y2); fclose(stdin); freopen("toys.out", "w", stdout); printf("%d\n", BFS(x, y, x2, y2, N)); fclose(stdout); return 0; }