UVA 11624 Fire! bfs

来源:互联网 发布:linux elinks命令 编辑:程序博客网 时间:2024/04/30 16:39

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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification


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.


Output for Sample Input


3
IMPOSSIBLE

Malcolm Sharpe, Ondřej Lhoták


题意:joe要逃出迷宫,迷宫内现在着火了,火往四个方向蔓延,每秒一个单位,joe每秒走一个单位。问joe能否逃生,若能的话输出最短时间。

思路:先对火源进行bfs处理一下,记录下每个格子最早的起火时间,再bfs判断能否逃出就好了。

另外就是这题其实可以只用一个队列bfs,初始化先把火源入队,再入队joe,然后加个flag判断是火还是人就行了。

代码:

#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>#include<queue>#include<stack>#include<set>#include<vector>#include<map>#define ll long long#define pi acos(-1)#define inf 0x3f3f3f3fusing namespace std;typedef pair<int,int>P;int n,m;char mp[1005][1005];bool book[1005][1005];int tp[1005][1005];int go[4][2]={1,0,0,1,-1,0,0,-1};struct node{int r,c;int time;}J;void fbfs(){queue<node>q;while(!q.empty())q.pop();for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(mp[i][j]=='F'){tp[i][j]=0;q.push({i,j,0});book[i][j]=1;}}}while(!q.empty()){node t=q.front(),x;q.pop();for(int i=0;i<4;i++){x.r=t.r+go[i][0];x.c=t.c+go[i][1];x.time=t.time+1;if(x.r<0||x.r>=n||x.c<0||x.c>=m||mp[x.r][x.c]=='#'||book[x.r][x.c])continue;if(tp[x.r][x.c]!=-1)continue;book[x.r][x.c]=1;tp[x.r][x.c]=x.time;q.push(x);}}}void bfs(){queue<node>q;while(!q.empty())q.pop();J.time=0;book[J.r][J.c]=1;q.push(J);while(!q.empty()){node t=q.front(),x;q.pop();if(t.r==0||t.r==n-1||t.c==0||t.c==m-1){printf("%d\n",t.time+1);return ;}for(int i=0;i<4;i++){x.r=t.r+go[i][0];x.c=t.c+go[i][1];x.time=t.time+1;if(x.r<0||x.r>=n||x.c<0||x.c>=m||mp[x.r][x.c]=='#'||book[x.r][x.c])continue;if(tp[x.r][x.c]<=x.time&&tp[x.r][x.c]!=-1)continue;book[x.r][x.c]=1;q.push(x);}}printf("IMPOSSIBLE\n");}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%s",mp[i]);for(int j=0;j<m;j++){if(mp[i][j]=='J'){J.r=i;J.c=j;}}}memset(tp,-1,sizeof(tp));memset(book,0,sizeof(book));fbfs();//for(int i=0;i<n;i++)//for(int j=0;j<m;j++)//printf("%d%c",tp[i][j]," \n"[j==m-1]);memset(book,0,sizeof(book));bfs();}    return 0;}

后记:

这题对我有毒,自己敲完怎么改都是wa,以为自己思路错了,看了题解发现思路一样,感觉也没有什么坑点啊,然而就是不对,后来发现自己和题解的主要不同就是bfs我写的if()则入队,而题解写的是if()continue(不入队),并且判断条件刚好相反,一开始觉得不是这地方的错就没管,后来wa急了就瞎改,然后我也改成continue就过了。。我不信邪以为是别的什么地方错了不小心改了,就又把判断条件改回去交,结果还是wa。。。真的有毒啊。。

把当时wa的判断条件放这里了,希望有路过的大神不吝赐教!

if(0<=x.r&&x.r<n&&0<=x.c&&x.c<m&&!book[x.r][x.c]&&mp[x.r][x.c]!='#'&&x.time<tp[x.r][x.c])

0 0
原创粉丝点击