kuangbin 简单搜索 J 双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

题解:

WA一次。原因是没考虑多个fire。
TLE一次。原因是一开始把所有fire起始状态存入vector,对每个fire起始状态进行bfs搜索。这样状态太多,直接把每个起始状态存入queue中,层次遍历即可。注意判断火蔓延顺序。
思路:
BFSF求出火势蔓延的顺序。
BFSJ求出J逃生的最短路,注意J去的地方必须在火势蔓延之前。
如果出不去,输出impossible.

代码:

#include <bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
struct Node
{
    int x,y;
    Node(int x,int y):x(x),y(y){}
    Node(){}
};

struct Status
{
    int ftime,steps;
    Status(int ftime,int steps):ftime(ftime),steps(steps){}
    Status(){}
};

const int maxn = 1000+10;
char maze[maxn][maxn];
Status d[maxn][maxn];

int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

int Jx,Jy;
int Fx,Fy;
int R,C;

void bfsJ()
{
   queue<Node> que;
   que.push(Node(Jx,Jy));
   d[Jx][Jy].steps=0;

   while(que.size())
   {
       Node s = que.front();
       que.pop();

       if(s.x==0||s.x==R-1||s.y==0||s.y==C-1)
       {
           cout<<d[s.x][s.y].steps+1<<endl;
           return;
       }

       for(int i=0;i<4;i++)
       {
           int nx = s.x+dir[i][0],ny=s.y+dir[i][1];
           if(nx>=0&&nx<R&&ny>=0&&ny<C&&maze[nx][ny]=='.'&&d[nx][ny].steps==INF&&d[nx][ny].ftime>d[s.x][s.y].steps+1)
           {
                d[nx][ny].steps=d[s.x][s.y].steps+1;
                que.push(Node(nx,ny));
           }
       }
   }
   printf("IMPOSSIBLE\n");
}

queue<Node> fire;

void bfsF()
{
    while(fire.size())
    {
       Node start = fire.front();
       fire.pop();

       for(int i=0;i<4;i++)
       {
           int nx = start.x+dir[i][0],ny=start.y+dir[i][1];

           if(nx>=0&&nx<R&&ny>=0&&ny<C&&maze[nx][ny]!='#'&&d[start.x][start.y].ftime+1<d[nx][ny].ftime)
           {
              d[nx][ny].ftime=d[start.x][start.y].ftime+1;

              fire.push(Node(nx,ny));
           }
       }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        while(!fire.empty()) fire.pop();
        memset(d,0x3f,sizeof(d));
        cin>>R>>C;
         for(int i=0;i<R;i++)
            for(int j=0;j<C;j++)
         {
             cin>>maze[i][j];
             if(maze[i][j]=='J')
             {
                 Jx=i;
                 Jy=j;
             }
             if(maze[i][j]=='F')
             {
                 fire.push(Node(i,j));
                 d[i][j].ftime=0;
             }
         }

              bfsF();

           /*for(int i=0;i<R;i++)
         {
             for(int j=0;j<C;j++)
         {
             if(d[i][j].ftime==INF)
                cout<<'#'<<" ";
                else
             cout<<d[i][j].ftime<<" ";
         }
         cout<<endl;
         }*/
         bfsJ();
         /*for(int i=0;i<R;i++)
         {
             for(int j=0;j<C;j++)
         {

             cout<<d[i][j].steps<<" ";
         }
         cout<<endl;
         }*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值