UVA - 11624 Fire!(广搜*2)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671点击打开链接

Joe works in a maze. Unfortunately, portions of the maze havecaught on fire, and the owner of the maze neglected to create a fireescape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the mazeare on fire, you must determine whether Joe can exit the maze beforethe fire reaches him, and how fast he can do it.Joe and the fire each move one square per minute, vertically orhorizontally (not diagonally). The fire spreads all four directionsfrom each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the firemay enter a square that is occupied by a wall.InputThe first line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. Thefollowing R lines of the test case each contain one row of the maze. Each of these lines contains exactlyC 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 fireThere will be exactly one J in each test case.OutputFor each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before thefire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.Sample Input24 4#####JF##..##..#3 3####J.#.FSample Output3IMPOSSIBLE


贼丑的题目 链接看图吧

大概就是火每单位时间都会往四周扩散

主人公要到达迷宫的边缘 并且不能碰到火

那么我们先对火进行记忆化的广搜 计算每个点火到达的时间

然后对主人公进行广搜 如果下一步将会碰到火 就无法通过 找到边缘就能记录最小值

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
char mmap[1111][1111];
int book[1111][1111];
int bookj[1111][1111];
struct xjy
{
    int x,y;
};
xjy b,e;
int n,m,k;
queue <xjy > q;
int ans=INT_MAX;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void bfs()
{
    while(!q.empty())
        q.pop();
    q.push(b);
    while(!q.empty())
    {
        xjy mid=q.front();
        q.pop();
        if((mid.x==1)||(mid.x==m)||(mid.y==1)||(mid.y==k))
        {
            ans=min(ans,bookj[mid.x][mid.y]+1);
        }
        for(int i=0;i<4;i++)
        {
            xjy midmid;
            midmid.x=mid.x+dir[i][0];
            midmid.y=mid.y+dir[i][1];
            if( mmap[midmid.x][midmid.y]!='#' &&( (bookj[mid.x][mid.y]+1)<bookj[midmid.x][midmid.y] ) && ( ( bookj[mid.x][mid.y]+1)<book[midmid.x][midmid.y] ) )
            {
                q.push(midmid);
                bookj[midmid.x][midmid.y]=bookj[mid.x][mid.y]+1;
            }
        }
    }
}
void firebfs()
{
    while(!q.empty())
    {
        xjy mid=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            xjy midmid;
            midmid.x=mid.x+dir[i][0];
            midmid.y=mid.y+dir[i][1];
            if(mmap[midmid.x][midmid.y]!='#'&&(book[mid.x][mid.y]+1)<book[midmid.x][midmid.y])
            {
                q.push(midmid);
                book[midmid.x][midmid.y]=book[mid.x][mid.y]+1;
            }
        }
    }
}
int main()
{

    scanf("%d",&n);
    while(n--)
    {
        ans=INT_MAX;
    memset(mmap,'#',sizeof(mmap));
    for(int i=0;i<=1001;i++)
        {
            for(int j=0;j<=1001;j++)
            {
                    book[i][j]=INT_MAX;
                    bookj[i][j]=INT_MAX;
            }
        }
    scanf("%d%d",&m,&k);
    getchar();
    for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=k;j++)
            {
                    scanf("%c",&mmap[i][j]);
                    if(mmap[i][j]=='J')
                    {
                        b.x=i;
                        b.y=j;
                        bookj[i][j]=0;
                    }
                    else if(mmap[i][j]=='F')
                    {
                        xjy mid;
                        mid.x=i;
                        mid.y=j;
                        q.push(mid);
                        book[i][j]=0;
                    }
            }getchar();
        }
        firebfs();
        bfs();
        if(ans!=INT_MAX)
            cout << ans <<endl;
        else
            cout  << "IMPOSSIBLE" << endl;
    }


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值