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
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
 
这道破题真是烦人啊,找bug找了好久,真的是要看清楚条件,首先这道题与以往的bfs题不同,人在动,火也在动,而且起火的地方不只一处,题目说了portions,一开始我忽略了,发现了这个条件可以对火的蔓延bfs一次看看火到达每一个格子需要多久,然后对人的行动队列搜索,如果能在火蔓延到之前赶到这个格子就可以走的。
还有就是了解图的范围最少只有一个格子。
 
 
代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
char pi[][];
int vis[][];
int fire[][];
struct que
{
int x,y,t;
}temp;
int dir[][]={,,,,,-,-,};
int main()
{
int T,m,n,sx=,sy=,flag; queue<que> q;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
cin>>T;
while(T--)
{
cin>>n>>m;
flag=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>pi[i][j];
fire[i][j]=;
}
}
while(!q.empty())q.pop();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(pi[i][j]=='J')sx=i,sy=j;
else if(pi[i][j]=='F')
{
fire[i][j]=;
temp.x=i,temp.y=j,temp.t=;
q.push(temp);
}
else if(pi[i][j]=='#')vis[i][j]=;
}
}
while(!q.empty())
{
for(int k=;k<;k++)
{
int tx=q.front().x+dir[k][];
int ty=q.front().y+dir[k][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty]||q.front().t+>=fire[tx][ty])continue;
temp.x=tx,temp.y=ty,temp.t=q.front().t+;
fire[tx][ty]=temp.t;
q.push(temp);
}
q.pop();
}
while(!q.empty())q.pop();
temp.x=sx,temp.y=sy,temp.t=;
vis[sx][sy]=;
q.push(temp);
while(!q.empty())
{
if(!q.front().x||!q.front().y||q.front().x==n-||q.front().y==m-)
{
flag=;
cout<<q.front().t+<<endl;
break;
}
for(int i=;i<;i++)
{
int tx=q.front().x+dir[i][];
int ty=q.front().y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||vis[tx][ty]||fire[tx][ty]<=q.front().t+)continue;
temp.x=tx,temp.y=ty,temp.t=q.front().t+;
vis[tx][ty]=;
q.push(temp);
}
q.pop();
}
if(flag==)cout<<"IMPOSSIBLE"<<endl;
}
}
05-11 16:21