Fire!
Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.
Input Specification
The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:
· #, a wall
· ., a passable square
· J, Joe's initialposition in the maze, which is a passable square
· F, a square that is onfire
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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.
Output for SampleInput
3
IMPOSSIBLE
1、火苗不止一个
2、火苗不一定能到达每个点,人也一样
比如这样:
思路:先一次优先队列广搜处火苗到达能到的点的最短时间,再一个广搜出人能最快跑出区域的最短时间。
这样直接写是不会T的,不用优先队列应该是稳T的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 1005;
typedef struct
{
int x;
int y;
int step;
} node;
char map[maxn][maxn];
int vis[maxn][maxn];
int num[maxn][maxn];
int ne[4][2] = {1,0,-1,0,0,1,0,-1};
int sx,sy;
int r,c;
priority_queue<node> q;
bool operator< (node a,node b)
{
return a.step> b.step;
}
void ff()//先让火苗蔓延
{
while(!q.empty())
{
node t = q.top();
q.pop();
for(int i = 0;i< 4;i++)
{
int tx = t.x+ne[i][0];
int ty = t.y+ne[i][1];
if(tx< 0||tx>= r||ty< 0||ty>= c||map[tx][ty]!= '.'||vis[tx][ty])
continue;
vis[tx][ty] = 1;
node temp;
temp.x = tx;
temp.y = ty;
temp.step = t.step+1;
q.push(temp);
num[tx][ty] = temp.step;
}
}
for(int i = 0;i< r;i++)
for(int j = 0;j< c;j++)
if(num[i][j] == 0)
num[i][j] = 1e7;
return ;
}
int bfs()//再让人尽量出去
{
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
node st;
st.step = 0;
st.x = sx;
st.y = sy;
vis[sx][sy] = 1;
for(int i = 0;i< 4;i++)
{
int tx = sx+ne[i][0];
int ty = sy+ne[i][1];
if(tx< 0||tx>= r||ty< 0||ty>= c)
return (st.step+1);
}
q.push(st);
while(!q.empty())
{
node t = q.top();
q.pop();
for(int i = 0;i< 4;i++)
{
int tx = t.x+ne[i][0];
int ty = t.y+ne[i][1];
if(tx< 0||tx>= r||ty< 0||ty>= c)
return (t.step+1);
if(map[tx][ty]!= '.'||vis[tx][ty]||t.step+1>= num[tx][ty])
continue;
vis[tx][ty] = 1;
node temp;
temp.x = tx;
temp.y = ty;
temp.step = t.step+1;
q.push(temp);
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
while(!q.empty())
q.pop();
memset(num,0,sizeof(num));
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
scanf("%d %d",&r,&c);
getchar();
for(int i = 0;i< r;i++)
{
for(int j = 0;j< c;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == 'J')
sx = i,sy = j;
if(map[i][j] == 'F')
{
node temp;
temp.x = i;
temp.y = j;
temp.step = 0;
q.push(temp);
num[i][j] = 0;
vis[i][j] = 1;
}
}
getchar();
}
ff();
int ans = bfs();
if(ans == 0)
printf("IMPOSSIBLE\n");
else
printf("%d\n",ans);
}
return 0;
}
ACM精写文章