1 /* 2 * 简单BFS 3 */ 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std; 10 11 const int N = 2005; 12 13 int map[N][N]; 14 bool vis[N][N]; 15 struct node { 16 int x; 17 int y; 18 int c; 19 }Q[N*N], s, e; 20 int front, rear; 21 int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 22 23 int BFS(int n, int m) { 24 node first, next; 25 memset(vis, true, sizeof(vis)); 26 front = rear = 0; 27 vis[s.x][s.y] = false; 28 Q[front++] = s; 29 while (rear < front) { 30 first = Q[rear++]; 31 if (first.x==e.x && first.y==e.y) return first.c; 32 for (int i=0; i<4; ++i) { 33 next.x = first.x + dir[i][0]; 34 next.y = first.y + dir[i][1]; 35 next.c = first.c + 1; 36 if (next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]&&map[next.x][next.y]==0) { 37 Q[front++] = next; 38 vis[next.x][next.y] = false; 39 } 40 } 41 } 42 return -1; 43 } 44 45 int main() { 46 int n, m; 47 while (scanf("%d%d", &n, &m) != EOF) { 48 for (int i=0; i<n; ++i) { 49 for (int j=0; j<m; ++j) scanf ("%d", &map[i][j]); 50 } 51 scanf ("%d%d%d%d", &s.x, &s.y, &e.x, &e.y); 52 --s.x, --s.y, --e.x, --e.y; 53 s.c = 0; 54 int ans = BFS(n, m); 55 if (ans < 0) printf ("No Answer!\n"); 56 else printf ("%d\n", ans); 57 } 58 return 0; 59 }