UVA

来源:互联网 发布:油画 知乎 编辑:程序博客网 时间:2024/06/05 05:35

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 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.
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#
#..#
#..#
3 3

#

J.

#.F
Sample Output
3
IMPOSSIBLE
一个迷宫着火了,里面有一个人,火和人都会往上下左右四个方向移动,有墙的地方和火经过的地方人都不能过。
先将所有的起火点放入队列,然后再把人的位置放入队列。用结构体记录位置,时间和区分火和人。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;struct node{    int x,y,time,id;};int n,m;int Map[1001][1001];char ch[1001];int dx[]={-1,1,0,0};int dy[]={0,0,-1,1};queue<node> q;int bfs(){    node a,b;    while(!q.empty())    {        a=q.front();        q.pop();        for(int i=0;i<4;i++)        {            b.x=a.x+dx[i];            b.y=a.y+dy[i];            b.time=a.time+1;            b.id=a.id;            if(b.id&&(b.x==-1||b.x==n||b.y==-1||b.y==m))//判断人走出迷宫的条件            {                return b.time;            }            if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&!Map[b.x][b.y])//火和人到达的地方            {                Map[b.x][b.y]=1;                q.push(b);            }        }    }    return -1;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        node s1,s2;        while(!q.empty())q.pop();//全局变量,一定记得清空队列        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        {            scanf("%s",ch);            for(int j=0;j<m;j++)            {                if(ch[j]=='.')                {                    Map[i][j]=0;                }                else if(ch[j]=='#')                {                    Map[i][j]=1;                }                else if(ch[j]=='F')                {                    Map[i][j]=1;                    s1.x=i,s1.y=j,s1.time=0,s1.id=0;                    q.push(s1);//将火的信息加入队列                }                else if(ch[j]=='J')                {                    Map[i][j]=1;                    s2.x=i,s2.y=j,s2.time=0,s2.id=1;                }            }        }        q.push(s2);//最后将人的信息加入队列        int ans=bfs();        if(ans!=-1)        {            printf("%d\n",ans);        }        else        {            printf("IMPOSSIBLE\n");        }    }    return 0;}
原创粉丝点击