UVA 11624 Fire!

来源:互联网 发布:python 验证码识别库 编辑:程序博客网 时间:2024/06/08 05:27
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 Specification

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.

Sample Input

24 4#####JF##..##..#3 3####J.#.F

Output Specification

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.

Output for Sample Input

3IMPOSSIBLE 


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int MAXN=1005;const int INF=0x3f3f3f3f;int n,m;int sx,sy;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};char map[MAXN][MAXN];int vis[MAXN][MAXN];int visp[MAXN][MAXN];int T[MAXN][MAXN];struct node{    int x,y,s;};queue<struct node>q;void BFS_Fire(){    int i;    while(!q.empty())    {        struct node t;        t=q.front();        q.pop();        for(i=0;i<=3;i++)        {            int xx=t.x+dir[i][0];            int yy=t.y+dir[i][1];            if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!vis[xx][yy]&&map[xx][yy]!='#')            {                vis[xx][yy]=1;                struct node f;                f.x=xx;                f.y=yy;                f.s=t.s+1;                T[xx][yy]=f.s;                q.push(f);            }        }    }}int BFS_Man(){    queue<struct node>qq;    while(!q.empty()) q.pop();    struct node s;    s.x=sx;    s.y=sy;    s.s=0;    qq.push(s);    visp[sx][sy]=1;    while(!qq.empty())    {        int i;        struct node t;        t=qq.front();        qq.pop();        if(t.x==0||t.x==n-1||t.y==0||t.y==m-1) return t.s+1;        for(i=0;i<=3;i++)        {            int xx=t.x+dir[i][0];            int yy=t.y+dir[i][1];            //if(xx==0||xx==n-1||yy==0||yy==m-1) return t.s+1;            if(t.s+1>=T[xx][yy]) continue;            if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!visp[xx][yy]&&map[xx][yy]!='#')            {                visp[xx][yy]=1;                struct node f;                f.x=xx;                f.y=yy;                f.s=t.s+1;                qq.push(f);            }        }    }    return -1;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        while(!q.empty()) q.pop();        memset(vis,0,sizeof(vis));        memset(visp,0,sizeof(visp));        memset(T,INF,sizeof(T));        int i,j;        scanf("%d%d",&n,&m);        for(i=0;i<=n-1;i++)        {            scanf("%s",map[i]);        }        for(i=0;i<=n-1;i++)        {            for(j=0;j<=m-1;j++)            {                if(map[i][j]=='J')                {                    sx=i;                    sy=j;                }                if(map[i][j]=='F')                {                    struct node t;                    t.x=i;                    t.y=j;                    t.s=0;                    vis[i][j]=1;                    T[i][j]=0;                    q.push(t);                }            }        }        BFS_Fire();        int ans=BFS_Man();        if(ans==-1) printf("IMPOSSIBLE\n");        else printf("%d\n",ans);    }    return 0;}



原创粉丝点击