莫名其妙的 runtime error。
代码:
#include<stdio.h> #include<string.h> #include<queue> #define _n printf("\n"); using namespace std; struct T{ int x; int y; int step; }; int dir[5][5] = {{1 , 0} , {-1 , 0} , {0 , 1} , {0 , -1}}; int t , n , m , sx , sy; char map[1005][1005]; int vis[1005][1005]; int time[1005][1005]; const int inf = 10000000; bool flag ; queue<T> q; void bfs1(){ T next , now; while(!q.empty()){ now = q.front(); q.pop(); for(int i = 0 ; i < 4 ; i ++){ next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.step = now.step + 1; if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && map[next.x][next.y] == '.' && time[next.x][next.y] > next.step){ time[next.x][next.y] = next.step; q.push(next); } } } return ; } void bfs(){ while(!q.empty()) q.pop(); T next , now; flag = false; now.x = sx; now.y = sy; now.step = 0; vis[sx][sy] = 1; q.push(now); while(!q.empty()){ now = q.front(); q.pop(); if(now.x <= 0 || now.y <= 0 || now.x >= n - 1 || now.y >= m - 1){ printf("%d\n" , now.step + 1); flag = true; return; } for(int i = 0 ; i < 4 ; i ++){ next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.step = now.step + 1; if(next.step < time[next.x][next.y] && map[next.x][next.y] == '.' && next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[next.x][next.y]){ q.push(next); vis[next.x][next.y] = 1; } } } return ; } int main(){ T node; scanf("%d" , &t); while(t --){ scanf("%d%d" , &n , &m); for(int i = 0 ; i < n ; i ++){ scanf("%s" , map[i]); } for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < m ; j ++){ vis[i][j] = 0; time[i][j] = inf; } } while(!q.empty()) q.pop(); for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < m ; j ++){ if(map[i][j] == 'F'){ node.x = i; node.y = j; node.step = 0; time[i][j] = 0; vis[i][j] = 1; q.push(node); } if(map[i][j] == 'J'){ sx = i; sy = j; } } } bfs1(); memset(vis,0,sizeof(vis)); bfs(); if(flag == false){ printf("IMPOSSIBLE\n"); } } return 0; }