I - Fire!(广度优先搜索+优先队列)

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

题目大意:一个人J和火堆F,每次移动一个格子,问人是否可以逃脱(不在图中),如果可以求出最短的步数

有结构体记录人的步数,用一个数组,记录火堆的步数,先求出火堆,然后看看人是否可以在火之前走出~

特别的,F可能有好几个~

#include 
#include 
#include 
#include 
#include 
#define MAXN 1056
using namespace std;
char ma[MAXN][MAXN];//图
int v[MAXN][MAXN];//是否访问过
int n, m;
int num[MAXN][MAXN];//记录火堆走到某一点的步数(最小)
struct node
{
    int x;//横坐标
    int y;//纵坐标
    int step;//人的步数
};
int a, b;//人的起点
int xx[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};//四个方向
priority_queue q;//优先队列q
bool operator< (node a, node b)//先输出步数最大的
{
    return a.step > b.step;
}
void bfs()//求火的步数
{
    while(!q.empty())
    {
        struct node t = q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tx = t.x + xx[i][0];
            int ty = t.y + xx[i][1];
          if(tx>=0&&tx=0&&ty'.'&&v[tx][ty]==0)
            {
                v[tx][ty] = 1;
                struct node temp;
                temp.x = tx;
                temp.y = ty;
                temp.step = t.step + 1;
                q.push(temp);
                num[tx][ty] = temp.step;
            }
        }
    }
   for(int i = 0;i< n;i++)//将所有火不能走的点全部改成最大值
        for(int j = 0;j< m;j++)
            if(num[i][j] == 0)
                num[i][j] = 1e7;
    return ;
}
int bfss()//人逃走
{
    memset(v, 0, sizeof(v));
    while(!q.empty())
        q.pop();
    struct node op;
    op.x = a;
    op.y = b;
    op.step = 0;
    v[a][b] = 1;
    q.push(op);
    while(!q.empty())
    {
        struct node w;
        w = q.top();
        q.pop();
         if(w.x<0||w.x>=n||w.y<0||w.y>=m)
            return w.step + 1;
        for(int i=0;i<4;i++)
        {
            int tx = w.x + xx[i][0];
            int ty = w.y + xx[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m)
            {
                return w.step+1;
            }
            if(ma[tx][ty]=='.'&&v[tx][ty]==0&&w.step+1//人的步数小于火的~
            {
                v[tx][ty] = 1;
                struct node temp;
                temp.x = tx;
                temp.y = ty;
                temp.step = w.step+1;
                 q.push(temp);
            }
        }
    }
    return 0;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        while(!q.empty())
            q.pop();
        for(int i=0;iscanf("%s", ma[i]);
        memset(v, 0, sizeof(v));
        memset(num, 0, sizeof(num));
        for(int i=0;ifor(int j=0;jif(ma[i][j]=='J')
                {
                    a = i;
                    b = j;
                }
                else if(ma[i][j]=='F')
                {
                    struct node temp;
                    temp.x = i;
                    temp.y = j;
                    temp.step = 0;
                    q.push(temp);
                    num[i][j] = 0;
                    v[i][j] = 1;
                }
            }
        }
        bfs();
        memset(v, 0, sizeof(v));
        int k = bfss();
        if(k)
            printf("%d\n", k);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}

你可能感兴趣的:(ACM训练+实训+大学编程练习)