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 

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 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<1<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=0&&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 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=0&&ny'#'&&d[start.x][start.y].ftime+11;

              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;ifor(int j=0;jcin>>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
         bfsJ();
         /*for(int i=0;i


    }
    return 0;
}

你可能感兴趣的:(搜索)