Uva 11624 - Fire!

来源:互联网 发布:云考勤软件 编辑:程序博客网 时间:2024/05/22 09:51
Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re

may enter a square that is occupied by a wall.


Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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



题意就是在火像四周扩展时,人也向四周跑 如果人能在火烧到他之前跑出去 就说明能够安全跑出去 就输出跑出去的最短步数

如果不能就输出impossible


 方法就是两次BFS 第一次BFS把火势延伸出去,然后用一个二维数组记录下火势延伸到这个点的时间

记录下时间后 再用第二次BFS 目的是用J 看能否判处去的BFS 注意要增加判断条件 

if 人走到这个点的时候的时间已经〉=二维数组记录火势蔓延到此点的时间 那么人就被烧到了

  如果小于二维数组记录的时间 人就可以继续广搜


最后交的时候一直WR 知道看了别人的代码中才发现 原来火种不止一个点 有可能有多个点 所以如果只是一个点扩展的话当然WR WTF。。。

AC code:

</pre><pre name="code" class="cpp">
#include<cstdio>#include<queue>#include<cstring>using namespace std;char a[1010][1010];int l,w;struct node//fire or the ecaper{int x,y;int step;};node J,F;bool bookf[1010][1010];bool bookj[1010][1010];int sp[1010][1010];int main(){int dir[][2]={{-1,0},{0,1},{1,0},{0,-1}};int n;scanf("%d",&n);while(n--){memset(a,0,sizeof(a));memset(bookf,0,sizeof(bookf));memset(bookj,0,sizeof(bookj));memset(sp,0,sizeof(sp));scanf("%d%d",&l,&w);getchar();queue<node>q;for(int i=1;i<=l;i++){for(int j=1;j<=w;j++){char c;c=getchar();if(c=='J'){J.x=i;J.y=j;}else if(c=='F'){F.x=i;F.y=j;F.step==0;bookf[F.x][F.y]=1;q.push(F);}a[i][j]=c;}getchar();}        if(J.x==l||J.y==w||J.x==1||J.y==1)        {            printf("1\n");            continue;        }int tx,ty;node n1,n2;while(q.size()){n1=q.front();q.pop();for(int i=0;i<4;i++){tx=n1.x+dir[i][0];ty=n1.y+dir[i][1];if(tx>0&&ty>0&&tx<=l&&ty<=w&&!bookf[tx][ty]&&a[tx][ty]!='#'){n2.x=tx;n2.y=ty;n2.step=n1.step+1;q.push(n2);sp[tx][ty]=n2.step;//°Ñ¸Ãµã±ê¼Ç³É ²½Êýbookf[tx][ty]=1;}}}bool flag=0;J.step=0;bookj[J.x][J.y]=1;q.push(J);int st;while(q.size()){n1=q.front();q.pop();for(int i=0;i<4;i++){tx=n1.x+dir[i][0];ty=n1.y+dir[i][1];st=n1.step+1; if(tx>0&&ty>0&&tx<=l&&ty<=w&&a[tx][ty]=='.'&&!bookj[tx][ty]){if(st<sp[tx][ty]||sp[tx][ty]==0){n2.x=tx;n2.y=ty;n2.step=st;bookj[tx][ty]=1;q.push(n2);}}if(tx>l||ty>w||tx<1||ty<1){flag=1;break;}}if(flag)                break;}if(flag)printf("%d\n",st);elseprintf("IMPOSSIBLE\n");}return 0;}


0 0
原创粉丝点击