Fire! 又是图 bfs

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.
Input
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 square
J, Joe's initial position in the maze, which is a passable square
F, a square that is on  re
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
 
这道破题真是烦人啊,找bug找了好久,真的是要看清楚条件,首先这道题与以往的bfs题不同,人在动,火也在动,而且起火的地方不只一处,题目说了portions,一开始我忽略了,发现了这个条件可以对火的蔓延bfs一次看看火到达每一个格子需要多久,然后对人的行动队列搜索,如果能在火蔓延到之前赶到这个格子就可以走的。
还有就是了解图的范围最少只有一个格子。
 
 
代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
char pi[1000][1000];
int vis[1000][1000];
int fire[1000][1000];
struct que
{
    int x,y,t;
}temp;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int main()
{
    int T,m,n,sx=0,sy=0,flag;

    queue<que> q;
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(0);
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        flag=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>pi[i][j];
                fire[i][j]=999999999;
            }
        }
        while(!q.empty())q.pop();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(pi[i][j]=='J')sx=i,sy=j;
                else if(pi[i][j]=='F')
                {
                    fire[i][j]=0;
                    temp.x=i,temp.y=j,temp.t=0;
                    q.push(temp);
                }
                else if(pi[i][j]=='#')vis[i][j]=1;
            }
        }
        while(!q.empty())
        {
            for(int k=0;k<4;k++)
            {
                int tx=q.front().x+dir[k][0];
                int ty=q.front().y+dir[k][1];
                if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty]||q.front().t+1>=fire[tx][ty])continue;
                temp.x=tx,temp.y=ty,temp.t=q.front().t+1;
                fire[tx][ty]=temp.t;
                q.push(temp);
            }
            q.pop();
        }
        while(!q.empty())q.pop();
        temp.x=sx,temp.y=sy,temp.t=0;
        vis[sx][sy]=1;
        q.push(temp);
        while(!q.empty())
        {
            if(!q.front().x||!q.front().y||q.front().x==n-1||q.front().y==m-1)
            {
                flag=1;
                cout<<q.front().t+1<<endl;
                break;
            }
            for(int i=0;i<4;i++)
            {
                int tx=q.front().x+dir[i][0];
                int ty=q.front().y+dir[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m||vis[tx][ty]||fire[tx][ty]<=q.front().t+1)continue;
                temp.x=tx,temp.y=ty,temp.t=q.front().t+1;
                vis[tx][ty]=1;
                q.push(temp);
            }
            q.pop();
        }
        if(flag==0)cout<<"IMPOSSIBLE"<<endl;
    }
}

 

转载于:https://www.cnblogs.com/8023spz/p/7242123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值