UVA

来源:互联网 发布:主机屋mysql数据库地址 编辑:程序博客网 时间:2024/06/16 09:30

UVA - 11624  Fire!

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能逃出去,能逃出去,输出所用时间。

用两次广搜,第一次对火进行广搜,记录一下火到每一个 . 所在的格子需要多长时间

第二次对人进行广搜,只是在扩展的时候多加了一个条件,每到一个可以走的格子,判断一下人所用的时间是否小于火蔓延到这里的时间,小于则可以走。

不要被样例给迷惑了,题目说了不止有一处起火,可能有多处起火,开始需要把所有起火的地方都加入队列里

这道题一开始做的时候在学校机房里弄了一下午,全是wa,几乎崩溃了,然后晚上到宿舍里重写了一遍ac了,还是要仔细呀以后

#include<stdio.h>#include<queue>#include<string.h>using namespace std;char map[1011][1011];int book_p[1011][1011];int book_f[1011][1011];int mi[1011][1011];int n,m;struct person{int x;int y;int minute;};struct fire{int x;int y;int minute;};int next1[4][2]={    {1,0},{0,1},{-1,0},{0,-1}};int sx,sy;void bfs_f(){struct fire temp;queue<fire> f;for(int i=0;i<n;i++){for(int j=0;j<m;j++){mi[i][j]=1000000000;if(map[i][j]=='F'){temp.x=i;temp.y=j;temp.minute=0;f.push(temp);book_f[i][j]=1;mi[i][j]=0;}}}struct fire a;while(!f.empty()){temp=f.front();f.pop();for(int i=0;i<4;i++){a=temp;a.x+=next1[i][0];a.y+=next1[i][1];a.minute+=1;if(a.x<0||a.y<0||a.x>=n||a.y>=m||book_f[a.x][a.y]||map[a.x][a.y]=='#'||map[a.x][a.y]=='F')  continue;f.push(a);book_f[a.x][a.y]=1;  mi[a.x][a.y]=a.minute;}}}int bfs_p(){int ans=0,flag=0;if(map[sx][sy]=='F')  return -1;struct person t1,t2,t3;queue<person> p;t1.x=sx;t1.y=sy;t1.minute=0;p.push(t1);book_p[sx][sy]=1;while(!p.empty()){t2=p.front();p.pop();for(int i=0;i<4;i++){t3=t2;t3.x+=next1[i][0];t3.y+=next1[i][1];t3.minute+=1;if(t3.x<0||t3.y<0||t3.x>=n||t3.y>=m){flag=1;ans=t3.minute;break;}if(map[t3.x][t3.y]=='#'||book_p[t3.x][t3.y]||map[t3.x][t3.y]=='F'||t3.minute>=mi[t3.x][t3.y])  continue;p.push(t3);book_p[t3.x][t3.y]=1;  }if(flag) break;}if(flag) return ans;else return -1;}int main(void){int ans;int T;scanf("%d",&T);while(T--){memset(book_f,0,sizeof(book_f));memset(book_p,0,sizeof(book_p));memset(mi,0,sizeof(mi));ans=0;scanf("%d%d",&n,&m);getchar();for(int i=0;i<n;i++){scanf("%s",map[i]);for(int j=0;map[i][j];j++){if(map[i][j]=='J'){sx=i;sy=j;}}}bfs_f();ans=bfs_p();if(ans!=-1) printf("%d\n",ans);else printf("IMPOSSIBLE\n"); }return 0;}