UVA-11624-Fire!(多点BFS)
2016-08-06 09:14
375 查看
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
题意:图中#代表墙.代表路,J代表人,F代表火,人和火每秒都可以上下左右一步,火烧过的地方,人不能再去,出口就在地图边界。
坑点:火可以有很多处,一千乘以一千的图要求一秒搜完,还是多组数据。
思路:接收数据时把火的坐标先入队,人的坐标暂时记录,等接收完了再最后入队。根据队列的先进后出,BFS时先出的是火的结点,把循环一遍当前层次结点看作一秒,那么当前秒,总是先记录火覆盖的位置,再判断人是否可走。这样不停的剪枝,一遍BFS即可
代码
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
题意:图中#代表墙.代表路,J代表人,F代表火,人和火每秒都可以上下左右一步,火烧过的地方,人不能再去,出口就在地图边界。
坑点:火可以有很多处,一千乘以一千的图要求一秒搜完,还是多组数据。
思路:接收数据时把火的坐标先入队,人的坐标暂时记录,等接收完了再最后入队。根据队列的先进后出,BFS时先出的是火的结点,把循环一遍当前层次结点看作一秒,那么当前秒,总是先记录火覆盖的位置,再判断人是否可走。这样不停的剪枝,一遍BFS即可
代码
#include<stdio.h> #include<algorithm> #include<iostream> #include<math.h> #include<string.h> #include<queue> using namespace std; struct node { int x; int y; int steap;//记录人的步数 int is_fire;//辨别这个节点是火还是人 }; queue<node>q; const int maxn=1005; char map[maxn][maxn]; int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int M,N; void BFS() { while(!q.empty()) { node star=q.front(); q.pop(); if(star.is_fire==0&&(star.x==0||star.x==M-1||star.y==0||star.y==N-1)) { printf("%d\n",star.steap+1); return; } for(int i=0;i<4;i++) { node end; end=star; end.x+=dis[i][0]; end.y+=dis[i][1]; if(end.x>=0&&end.x<M&&end.y>=0&&end.y<N&&map[end.x][end.y]=='.') { if(end.is_fire==0) end.steap++; map[end.x][end.y]='#'; q.push(end); } } } printf("IMPOSSIBLE\n"); } int main() { int T; scanf("%d",&T); while(T--) { while(!q.empty()) q.pop();//清空队列 node point;//用来将火的坐标,人的坐标入队 int star_x,star_y;//人的坐标 scanf("%d%d",&M,&N); for(int i=0;i<M;i++) { scanf("%s",map[i]); for(int j=0;j<N;j++) { if(map[i][j]=='F') { point.x=i; point.y=j; point.is_fire=1; q.push(point); } else if(map[i][j]=='J') { star_x=i; star_y=j;//人的坐标等到最后再入队 } } } point.x=star_x; point.y=star_y; point.steap=0; point.is_fire=0; q.push(point); BFS(); } return 0; }
相关文章推荐
- UVA - 11624 Fire! (2次bfs)
- UVA 11624 Fire bfs水题
- uva11624 Fire! (双bfs)
- uva 11624 - Fire!(BFS)
- UVA - 11624 Fire! (两次BFS)
- uva 11624 Fire!(bfs)
- UVA 11624 Fire!(BFS)
- uva 11624 Fire!(bfs预处理)
- UVA 11624 Fire!【BFS】
- UVA 11624 Fire! (BFS)
- 【UVA】 11624 Fire!【BFS】
- bfs别忘啊...UVA11624Fire!(BFS) 止めないで//车轮的bfs /UVA10047
- UVA - 11624 Fire! BFS
- Uva-11624(Fire!)——BFS
- UVA 11624 Fire!(bfs)
- UVA 11624 Fire! 【特殊BFS】
- Uva11624 Fire(bfs 搜索)
- UVA 11624 Fire! (两次BFS 火追人 )
- UVA 11624 Fire!(图论BFS)
- UVA 11624 Fire! BFS搜索