简单搜索--J - Fire!( 双 bfs )

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
2
4 4

#JF#
#…#
#…#
3 3

#J.
#.F
Sample Output
3
IMPOSSIBLE

思路:把火到每个点的最少时间bfs出来,在bfs人走,只有人到某点走的时间小于火到某点走的时间才可以
注,搜索的一些判断条件要根据题意放到恰当的地方,像如果J给出就是边界点,那么对于结束的判定一定要放在循环的外面,不然就找不到了。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1005;

char s[N][N];
bool vis[N][N];
int xdist[N][N];
int ydist[N][N];
typedef struct Node{
    int x,y;
    int num;
}Node;
int n,m;
int MAX;
int col[5] = {0,0,1,-1};
int con[5] = {1,-1,0,0};

bool judge(int i,int j)
{
    if(i < 0 || j < 0)
        return false;
    if(i >= n || j >= m)
        return false;
    return true;
}

void bfs(int sx,int sy)
{
    memset(vis,false,sizeof(vis));
    queue<Node>que;
    que.push((Node){sx,sy,0});
    vis[sx][sy] = true;
    bool flag = false;
    while(!que.empty())
    {
        Node tmp = que.front();
        que.pop();
        int x = tmp.x,y = tmp.y;
        //把结束判断放到下面最后一个if语句里面就错了
        //因为如果J起初就在边界上,那就找不到了
        if(x == 0 || y == 0 || x == n - 1 || y == m - 1){
            printf("%d\n",tmp.num + 1);
            flag = true;
            break;
        }
        for(int i = 0;i < 4;++i)
        {
            int x1 = x + col[i],y1 = y + con[i];
            if(judge(x1,y1) && !vis[x1][y1]){
                    //cout << x1 << " " << y1 << endl;
                if(s[x1][y1] != '#' && (tmp.num + 1) < ydist[x1][y1]){
                    vis[x1][y1] = true;
                    xdist[x1][y1] = tmp.num + 1;
                    que.push((Node){x1,y1,tmp.num + 1});
                }
            }
        }
        if(flag) break;
    }
    if(!flag){
        printf("IMPOSSIBLE\n");
    }
}

void bfsfire()
{
    memset(vis,false,sizeof(vis));
    queue<Node>que;
    for(int i = 0;i < n;++i)
        for(int j = 0;j < m;++j)
            if(s[i][j] == 'F') ydist[i][j] = 0,que.push((Node){i,j,0}),vis[i][j] = true;
    while(!que.empty())
    {
        Node tmp = que.front();
        que.pop();
        int x = tmp.x,y = tmp.y;
        for(int i = 0;i < 4;++i)
        {
            int x1 = x + col[i],y1 = y + con[i];
            if(judge(x1,y1) && !vis[x1][y1]){
                    //cout << x1 << " " << y1 << endl;
                if(s[x1][y1] != '#'){
                    vis[x1][y1] = true;
                    ydist[x1][y1] = tmp.num + 1;
                    que.push((Node){x1,y1,tmp.num + 1});
                }
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        MAX = max(n,m);
        int sx,sy;
        int fx,fy;
        memset(xdist,inf,sizeof(xdist));
        memset(ydist,inf,sizeof(ydist));
        for(int i = 0;i < n;++i){
            scanf("%s",s[i]);
            for(int j = 0;j < m;++j){
                if(s[i][j] == 'J') sx = i,sy = j,xdist[i][j] = 0;
            }
        }
        bfsfire();
        bfs(sx,sy);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值