标签:
按白书上说的,先用一次bfs,求出每个点起火的时间
再bfs一次求出是否能够走出迷宫
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<vector> 7 using namespace std; 8 9 const int maxn = 1005; 10 const int INF = 1<<30 -1; 11 int r,c;//r行,c列 12 int d[maxn][maxn]; 13 int vis[maxn][maxn]; 14 char g[maxn][maxn]; 15 int sx,sy; 16 17 int dir[4][2] = {1,0,-1,0,0,1,0,-1}; 18 19 struct node{ 20 int x,y; 21 int step; 22 }p[maxn]; 23 24 queue<node> Q; 25 26 void bfs1(){ 27 memset(vis,0,sizeof(vis)); 28 while(!Q.empty()){ 29 node v = Q.front();Q.pop(); 30 for(int i = 0;i < 4;i++){ 31 int xx = v.x + dir[i][0]; 32 int yy = v.y + dir[i][1]; 33 if(xx < 1 || xx > r || yy < 1 || yy > c || vis[xx][yy] || g[xx][yy] != ‘.‘) continue; 34 vis[xx][yy] = 1; 35 d[xx][yy] = min(d[xx][yy],v.step+1); 36 Q.push(node{xx,yy,v.step+1}); 37 } 38 } 39 } 40 41 void bfs2(){ 42 queue<node> q; 43 memset(vis,0,sizeof(vis)); 44 q.push(node{sx,sy,0});vis[sx][sy] = 1; 45 46 while(!q.empty()){ 47 node u = q.front();q.pop(); 48 if(u.x == r || u.y == c || u.x == 1 || u.y == 1){ 49 printf("%d\n",u.step + 1); 50 return; 51 } 52 for(int i = 0;i < 4;i++){ 53 int xx = u.x + dir[i][0]; 54 int yy = u.y + dir[i][1]; 55 if(xx < 1 || xx > r || yy < 1 || yy > c || vis[xx][yy] || g[xx][yy] != ‘.‘) continue; 56 if(u.step + 1 >= d[xx][yy]) continue; 57 vis[xx][yy] = 1; 58 q.push(node{xx,yy,u.step+1}); 59 } 60 } 61 puts("IMPOSSIBLE"); 62 } 63 64 int main(){ 65 int T; 66 scanf("%d",&T); 67 while(T--){ 68 while(!Q.empty()) Q.pop(); 69 scanf("%d %d",&r,&c); 70 for(int i = 1;i <= r;i++){ 71 for(int j = 1;j <= c;j++) d[i][j] = INF; 72 } 73 for(int i = 1;i <= r;i++){ 74 for(int j = 1;j <= c;j++) { 75 cin>>g[i][j]; 76 if(g[i][j] == ‘F‘) Q.push(node{i,j,0}); 77 if(g[i][j] == ‘J‘) sx = i,sy = j; 78 } 79 } 80 bfs1(); 81 bfs2(); 82 } 83 return 0; 84 }
加油~~~gooooooo~~
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/4711625.html