Fire! UVA - 11624 (两次BFS,也有一次BFS的解法)
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
题目大意:Joe在一个迷宫里工作,然后迷宫着火了,火扩散的速度和Joe移动的速度一样都是每分钟一格,'#'代表着墙,而Joe和火都只能走'.'所代表的广场,问Joe能否在火烧到他之前逃出迷宫。
思路两次bfs:将火蔓延的时间进行预处理,用一次BFS搜索出火到达每一个广场所需要的最短时间,然后再BFS人到各个广场所需要的时间,如果人到某一个地点的时间少于火到达这里的这时间,或者人到达的某一个地点,火烧不到,就可以继续搜索,直到Joe到矩阵的边界为止
坑点:1.火'F'的位置不止一个。
代码一:用火的时间去检查人
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int maxn = 1005; 8 const int inf = 0x3f3f3f3f; 9 typedef pair<int,int> P; 10 int sx,sy,R,C,T,cnt,final; 11 int s[maxn][maxn],judge[maxn][maxn]; 12 char map[maxn][maxn]; 13 int dx[4] = {0,1,-1,0}; 14 int dy[4] = {1,0,0,-1}; 15 16 void Joe_BFS(){ 17 int f; 18 queue<P>que; 19 que.push(P(sx,sy)); 20 s[sx][sy] = 0; 21 while(que.size()){ 22 P p = que.front(); 23 que.pop(); 24 for(int i = 0 ; i < 4 ; i++){ 25 int nx = p.first + dx[i]; 26 int ny = p.second + dy[i]; 27 if(nx >= 0 && ny >= 0 && ny < C && nx < R && map[nx][ny] == '.' && s[nx][ny] == inf){ 28 que.push(P(nx,ny)); 29 s[nx][ny] = s[p.first][p.second] + 1; 30 } 31 } 32 } 33 } 34 void Fire_BFS(){ 35 memset(judge,inf,sizeof(judge)); 36 queue<P>que; 37 for(int i = 0 ; i < R ; i++){ 38 for(int j = 0 ; j < C ; j++){ 39 if(map[i][j] == 'F'){ 40 que.push(P(i,j)); 41 judge[i][j] = 0; 42 } 43 } 44 } 45 while(que.size()){ 46 P p = que.front(); 47 que.pop(); 48 for(int i = 0 ; i < 4 ; i++){ 49 int nx = p.first + dx[i]; 50 int ny = p.second + dy[i]; 51 if(nx>= 0 && ny >= 0 && ny < C && nx < R && map[nx][ny] != '#' && judge[nx][ny] == inf){ 52 judge[nx][ny] = judge[p.first][p.second] + 1; 53 if(judge[nx][ny] <= s[nx][ny] ) s[nx][ny] = -1; 54 que.push(P(nx,ny)); 55 } 56 } 57 } 58 } 59 60 int main(){ 61 ios::sync_with_stdio(false); 62 cin>>T; 63 while(T--){ 64 final = inf; 65 memset(s,inf,sizeof(s)); 66 memset(map,0,sizeof(map)); 67 cnt = 0; 68 cin>>R>>C; 69 for(int i = 0 ; i < R ; i++){ 70 for(int j = 0 ; j < C; j++){ 71 cin>>map[i][j]; 72 //if(j == C - 1) getchar(); 73 if(map[i][j] == 'J'){ 74 sx = i; 75 sy = j; 76 //J_BFS(); 77 } 78 } 79 } 80 Joe_BFS(); 81 Fire_BFS(); 82 int flag = 0; 83 for(int i = 0 ; i < R ; i++){ 84 for(int j = 0 ; j < C ; j++){ 85 if(i == R - 1 || j == C - 1|| i == 0 || j == 0){ 86 if(s[i][j] != -1 && s[i][j] != inf){ 87 final = min(final,s[i][j]); 88 } 89 } 90 } 91 } 92 if(final != inf) printf("%d\n",final + 1); 93 else printf("IMPOSSIBLE\n"); 94 } 95 return 0; 96 }
代码二:用人的时间去检查火
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int maxn = 1005; 8 const int inf = 0x3f3f3f3f; 9 typedef pair<int,int> P; 10 int sx,sy,R,C,T,cnt,final; 11 int s[maxn][maxn],judge[maxn][maxn]; 12 char map[maxn][maxn]; 13 int gx[maxn],gy[maxn]; 14 int dx[4] = {0,1,-1,0}; 15 int dy[4] = {1,0,0,-1}; 16 17 int Joe_BFS(){ 18 queue<P>que; 19 que.push(P(sx,sy)); 20 s[sx][sy] = 0; 21 while(que.size()){ 22 P p = que.front(); 23 que.pop(); 24 int tx = p.first; 25 int ty = p.second; 26 if(tx == 0 || ty == 0 || tx == R - 1 || ty == C - 1){ 27 return s[p.first][p.second] + 1; 28 } 29 for(int i = 0 ; i < 4 ; i++){ 30 int nx = p.first + dx[i]; 31 int ny = p.second + dy[i]; 32 if(nx >= 0 && ny >= 0 && ny < C && nx < R && map[nx][ny] == '.' && s[nx][ny] == inf && (judge[nx][ny] == inf || judge[nx][ny] > s[p.first][p.second] + 1)){ 33 que.push(P(nx,ny)); 34 s[nx][ny] = s[p.first][p.second] + 1; 35 } 36 } 37 } 38 return -1; 39 } 40 void Fire_BFS(){ 41 memset(judge,inf,sizeof(judge)); 42 queue<P>que; 43 for(int i = 0 ; i < R ; i++){ 44 for(int j = 0 ; j < C ; j++){ 45 if(map[i][j] == 'F'){ 46 que.push(P(i,j)); 47 judge[i][j] = 0; 48 } 49 } 50 } 51 while(que.size()){ 52 P p = que.front(); 53 que.pop(); 54 for(int i = 0 ; i < 4 ; i++){ 55 int nx = p.first + dx[i]; 56 int ny = p.second + dy[i]; 57 if(nx>= 0 && ny >= 0 && ny < C && nx < R && map[nx][ny] != '#' && judge[nx][ny] == inf){ 58 judge[nx][ny] = judge[p.first][p.second] + 1; 59 que.push(P(nx,ny)); 60 } 61 } 62 } 63 } 64 65 int main(){ 66 ios::sync_with_stdio(false); 67 cin>>T; 68 while(T--){ 69 final = inf; 70 memset(s,inf,sizeof(s)); 71 memset(map,0,sizeof(map)); 72 cnt = 0; 73 cin>>R>>C; 74 for(int i = 0 ; i < R ; i++){ 75 for(int j = 0 ; j < C; j++){ 76 cin>>map[i][j]; 77 //if(j == C - 1) getchar(); 78 if(map[i][j] == 'J'){ 79 sx = i; 80 sy = j; 81 //J_BFS(); 82 } 83 } 84 } 85 Fire_BFS(); 86 int final = Joe_BFS();; 87 if(final != -1) printf("%d\n",final); 88 else printf("IMPOSSIBLE\n"); 89 } 90 return 0; 91 }
还有一次BFS的写法,但是博主实在太菜了,写不出来,这里借用一下mza学姐的代码,以后自己写出来在换出来
我认为有问题的点:一次BFS应该先搜索火在搜索人。
个人观点:如果先搜索人的话,产生很多无用的情况(人已经被火烧到了),所以有可能会超时
火的扩散可以影响人的线路,但是人的行动不会影响火势的扩散。
附上代码:(我一定会写出来的,一次BFS写法)
1 #include<iostream> 2 #include<algorithm> 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<memory.h> 6 #include<queue> 7 #include<stack> 8 #include<map> 9 #include<cmath> 10 #include<string.h> 11 using namespace std; 12 const int INF = 0x3f3f3f3f; 13 #define pi acos(-1.0) 14 typedef long long ll; 15 #define esp 1e-7 16 const int maxn=1010; 17 int n,m; 18 int a[maxn]; 19 int dis[4][2]={0,1,0,-1,1,0,-1,0}; 20 char mp[maxn][maxn]; 21 int jx,jy,Fx,Fy; 22 int vis[maxn][maxn]; 23 int sp[maxn]; 24 struct Node 25 { 26 int x; 27 int y; 28 int co; 29 int flag; 30 }; 31 queue<Node> q; 32 Node st,now; 33 bool judge(int x,int y) 34 { 35 if(x<=n&&x>0&&y<=m&&y>0&&vis[x][y]==0) 36 return true; 37 else 38 return false; 39 } 40 int bfs() 41 { 42 while(!q.empty()) 43 { 44 now=q.front(); 45 q.pop(); 46 for(int i=0;i<4;i++) 47 { 48 st.x=now.x+dis[i][0]; 49 st.y=now.y+dis[i][1]; 50 st.co=now.co+1; 51 st.flag=now.flag; 52 if(st.flag&&(st.x==n+1||st.x==0||st.y==m+1||st.y==0)) 53 return st.co; 54 if(judge(st.x,st.y)) 55 { 56 vis[st.x][st.y]=1; 57 q.push(st); 58 } 59 } 60 } 61 return -1; 62 } 63 int main() 64 { 65 int t; 66 cin>>t; 67 while(t--) 68 { 69 cin>>n>>m; 70 while(!q.empty()) 71 q.pop(); 72 for(int i=1;i<=n;i++) 73 { 74 cin>>mp[i]; 75 } 76 for(int i=1;i<=n;i++) 77 { 78 for(int j=0;j<m;j++) 79 { 80 if(mp[i][j]=='.') 81 { 82 vis[i][j+1]=0; 83 } 84 else if(mp[i][j]=='#') 85 { 86 vis[i][j+1]=1; 87 } 88 else if(mp[i][j]=='J') 89 { 90 vis[i][j+1]=1; 91 jx=i; 92 jy=j+1; 93 } 94 else 95 { 96 vis[i][j+1]=1; 97 now.x=i; 98 now.y=j+1; 99 now.co=0; 100 now.flag=0; 101 q.push(now); 102 } 103 } 104 } 105 now.x=jx,now.y=jy,now.co=0,now.flag=1; 106 q.push(now); 107 int ans=bfs(); 108 if(ans==-1) 109 cout<<"IMPOSSIBLE"<<endl; 110 else 111 cout<<ans<<endl; 112 }
一个从很久以前就开始做的梦。

浙公网安备 33010602011771号