UVA_11624_Fire!

来源:互联网 发布:高校网络工程项目 编辑:程序博客网 时间:2024/06/06 02:05

11624 - Fire!

Time limit: 1.000 seconds

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
re 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

这个题目只需要把火加入bfs的队列就可以了

需要思考的就是火应该在人之后bfs 然后火和人如果同时到达一个格子

那么这个格子人并不能到

bfs的过程只要对应的填入人和火就可以了

同时到的会被填成火 然后搜索第一次扩展火,第二次不扩展因为周围已经都有火了

最后地图周围为弹出条件

#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;const int M=1005;char ti[M][M];int isu[M][M];queue<int> re;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};int bfs(int n,int m){    while(!re.empty())        re.pop();    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)            if(ti[i][j]=='J')            {                re.push(i*M+j);                isu[i][j]=1;            }    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)            if(ti[i][j]=='F')                re.push(i*M+j);    while(!re.empty())    {        int t=re.front();        re.pop();        int tx=t/M,ty=t%M;        //cout<<tx<<" "<<ty<<endl;        for(int i=0;i<4;i++)        {            int x=tx+dx[i],y=ty+dy[i];            if(ti[tx][ty]=='J')            {                if(ti[x][y]==0)                    return isu[tx][ty];                else if(ti[x][y]=='.'&&isu[x][y]==-1)                {                    re.push(x*M+y);                    ti[x][y]='J';                    isu[x][y]=isu[tx][ty]+1;                }            }            else            {                if(ti[x][y]==0||ti[x][y]=='F'||ti[x][y]=='#')                    continue;                else                {                    re.push(x*M+y);                    ti[x][y]='F';                    isu[x][y]=0;                }            }        }    }    return 0;}int main(){    int t;    int n,m;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        memset(isu,-1,sizeof(isu));        memset(ti,0,sizeof(ti));        for(int i=1;i<=n;i++)            scanf("%s",ti[i]+1);        int ans=bfs(n,m);        if(!ans)            printf("IMPOSSIBLE\n");        else            printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击