(UVA 11624)Fire!

题目链接 http://vjudge.net/contest/121377#problem/J

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 ≤ R, C ≤ . 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 ####
#JF#
#..#
#..#
###
#J.
#.F
Sample Output IMPOSSIBLE

题意:有个人在一个R*C的迷宫里,迷宫的某些点有火源,以每秒一格的速度向四周扩散,墙过不去,给出人的坐标,走到边缘就算逃出,问这个人是否能成功逃出,如逃出输出需要几秒?如不能输出-1;

方法:先让所有的火向四周扩散,再看这个人是否能逃出去

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 1009
int w[N][N],ww[N][N];
int a[][]={{,},{-,},{,},{,-}};
char str[N][N];
int n,m;
struct node
{
int x,y,s;
};
queue<node> Q;
node e,f,q,p;
void qq()///求火能蔓延的到所能蔓延的点得时间
{
memset(ww,INF,sizeof(ww));
while(Q.size())
{
p=Q.front();Q.pop();
ww[p.x][p.y]=p.s;
for(int i=;i<;i++)
{
q.x=p.x+a[i][];
q.y=p.y+a[i][];
q.s=p.s+;
if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&str[q.x][q.y]!='#'&&!w[q.x][q.y])
{
w[q.x][q.y]=;
Q.push(q);
}
}
}
}
int dfs()///人从起点开始走所有点遍历,看是否是到出口
{
qq();
memset(w,,sizeof(w));
queue<node> o;
o.push(f);
while(o.size())
{
p=o.front();o.pop();
if(p.x==||p.x==n-||p.y==||p.y==m-)
return p.s+;
for(int i=;i<;i++)
{
q.x=p.x+a[i][];
q.y=p.y+a[i][];
q.s=p.s+;
if(q.x>=&&q.x<n&&q.y>=&&q.y<m&&!w[q.x][q.y]
&&str[q.x][q.y]=='.'&&q.s<ww[q.x][q.y])
{
w[q.x][q.y]=;
o.push(q);
}
}
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
met(w,);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",str[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='F')///不止一个火源,找到一个存一个
{
e.x=i;e.y=j;e.s=;
w[i][j]=;Q.push(e);
}
if(str[i][j]=='J')
{
f.x=i;f.y=j;
f.s=;
}
}
}
int ans;
ans=dfs();
if(ans==-) printf("IMPOSSIBLE\n");
else
printf("%d\n",ans);
}
return ;
}
上一篇:nuget pack 时不包含依赖包(而不是引用项目的dll,区别于IncludeReferencedProjects)


下一篇:jQuery.form 上传文件