UVA11624 Fire!


Fire


<span style="font-size:14px;">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.</span>
<span style="font-size:18px;"><strong>Input</strong></span>
<span style="font-size:14px;">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 squareJ, Joe's initial position in the maze, which is a passable squareF, 
a square that is on  re There will be exactly oneJ in each test case.</span>
<span style="font-size:18px;"><strong>Output</strong></span>
<span style="font-size:14px;">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.</span>
<span style="font-size:18px;"><strong>Sample Input</strong></span>
<span style="font-size:14px;">2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F</span>

<span style="font-size:18px;"><strong>Sample Output</strong></span>
<span style="font-size:14px;">3
IMPOSSIBLE</span>


<pre name="code" class="cpp"><span style="font-size:14px;">#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int n,m;  //地图大小
int sx,sy;  //起始位置
int map[1002][1002];   //地图存储。“-1”是不能走,“-2” 是能走,“0-x”是火蔓延过来的时间
int dir[4][2]={1,0,0,1,-1,0,0,-1};  //四个方向
struct node   //搜索结构体
{
    int x;
    int y;
    int step;
}s,e;
queue<node> q;  //搜索共用队列
void initial()   //地图,队列初始化
{
    int i,j;
    char x[1002];   //暂存数组
    memset(map,-1,sizeof(map));   //全部初始化为0
    while(!q.empty())q.pop();   //情况队列
    scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)   //输入地图,并处理
    {
        scanf("%s",x);
        for(j=0;j<m;j++)
        {
            if(x[j]=='.')map[i][j]=-2;   //此处可走
            else if(x[j]=='F')   //此处有火
            {
                map[i][j]=0;
                s.x=i;
                s.y=j;
                s.step=0;
                q.push(s);
            }
            else if(x[j]=='J')sx=i,sy=j;    //起点
        }   //由于最开始全部赋值为-1,都默认为不能走,所以,输入为“#”时,不用再处理
    }
    while(!q.empty())   //广度优先搜索,确定火蔓延到每个点的时间【可能蔓延不到】
    {
        s=q.front();
        q.pop();
		for(i=0;i<4;i++)//四个方向搜索
        {
            e.x=s.x+dir[i][0];
            e.y=s.y+dir[i][1];
            e.step=s.step+1;
            if(e.x<0||e.x>=n||e.y<0||e.y>=m||map[e.x][e.y]!=-2)continue;   //该点在地图外面或该点不可被蔓延,不处理该点
            map[e.x][e.y]=e.step;   //记录蔓延过来的时间
            q.push(e);
        }
	 }
}
int BFS()  //逃亡广搜,能逃亡成功就返回时间,否则,返回-1
{
    s.x=sx;
    s.y=sy;
    s.step=1;
    map[sx][sx]=-1;
    q.push(s);  //起点入队
    while(!q.empty())
    {
		s=q.front();
        if(s.x==n-1||s.x==0||s.y==m-1||s.y==0) return s.step;   //起点要判断,放这里更合适【这点的疏忽,让我郁闷了三天】逃亡成功
        q.pop();
        for(int i=0;i<4;i++)   //四个方向搜索
        {
            e.x=s.x+dir[i][0];
            e.y=s.y+dir[i][1];
            e.step=s.step+1;
            if(e.x<0||e.x>=n||e.y<0||e.y>=m||(map[e.x][e.y]!=-2&&map[e.x][e.y]<e.step))continue;  //不可走,不处理
			 map[e.x][e.y]=-1;
            q.push(e);
        }
    }
    return -1;   //所有能走的地方都走了,还是没有逃出去。逃亡失败
}
int main()
{
    int t;
    scanf("%d",&t);   //案例测试数据组数
    while(t--)
    {
        initial();   //输入,赋值处理等初始化
        int ans=BFS();   //逃亡广搜
		if(ans==-1)
		printf("IMPOSSIBLE\n");   //结果分析,所有能走的节点都走过了,还没有逃出迷宫
        else 
		printf("%d\n",ans);
    }
    return 0;
}</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值