Problem B: 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
/* 普通的宽度搜索题 先预处理一下map,找出每个点的着火时间,然后再次宽度搜索 如果人走到那一点的时间小于着火时间,才能入队。 */ #include<cstdio> #include<queue> #include<iostream> #include<cstring> using namespace std; const int M=1002; char map[M][M];//存放地图 int newmap[M][M];//存放地图上的点的着火时间 int fang[4][2]={0,1,0,-1,1,0,-1,0};//方向数组 struct node { int x; int y; int step;//记录步数 }J,F,term; queue<node>q; int R,C; int main() { int T,i,j,flag; scanf("%d",&T); while(T--) { while(!q.empty())q.pop(); memset(newmap,0,sizeof(newmap)); memset(map,0,sizeof(map)); scanf("%d%d",&R,&C); for(i=1;i<=R;i++) { for(j=1;j<=C;j++) { cin>>map[i][j]; if(map[i][j]=='J') { J.x=i; J.y=j; J.step=0; } if(map[i][j]=='F') { F.x=i; F.y=j; map[i][j]='#'; q.push(F);//着火点不止一个 } } } while(!q.empty()) { term=q.front(); q.pop(); for(i=0;i<4;i++) { node t; t.x=term.x+fang[i][0]; t.y=term.y+fang[i][1]; if(map[t.x][t.y]==0||map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&(newmap[t.x][t.y]<=newmap[term.x][term.y]+1))continue; newmap[t.x][t.y]=newmap[term.x][term.y]+1; q.push(t); } } q.push(J); flag=0; while(!q.empty()) { term=q.front(); q.pop(); for(i=0;i<4;i++) { node t; t.x=term.x+fang[i][0]; t.y=term.y+fang[i][1]; t.step=term.step+1; if(map[t.x][t.y]==0){flag=1;break;}//逃出 if(map[t.x][t.y]=='#'||newmap[t.x][t.y]!=0&&newmap[t.x][t.y]<=t.step)continue; map[t.x][t.y]='#'; q.push(t); } if(flag)break; } if(flag==1)printf("%d\n",term.step+1); else printf("IMPOSSIBLE\n"); } return 0; }
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
uva532 Dungeon Master的源代码,并且AC了
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
tpcw-nyu-uva-client 客户端
UVa Online Judge(ACM-ICPC Live Archive)、hackerrank、Leetcode.com的算法问题解决方案 使用:数组、哈希表、链表、二分搜索、动态规划、堆栈、堆、reedy、排序、树 DFS、BFS、图、二分搜索树、递归、记忆、队列...