G - Fire! UVA 11624 (BFS)

解题过程:

首先一开始做的时候各种粗心大意,导致各种错误,后来把程序搞出来以后提交,TLE,那就优化,还是TLE,各种优化,各种TLE,这当中还在不断地改错,挑错,那个心塞的感觉,简直不要不要的,然后就愤怒的把代码全删了(主要是各种改,到处是注释掉的代码和调试代码,弄得代码糟了吧唧的,看着眼晕),后来就直接一口气想好,直接上代码,改了一个小错后,认为一定可以AC然后提交,结果WA了, 后来发现忘了题目中的初始的火是可以有多个的,然后就搞了个循环,把初始点火一个个的循环在比较时间,然后不出意料TLE了,最后一次改对了 提交AC,下面就说说我最后是怎么AC的。



解题思路:

1,首先题目中给的R,C的范围是小于一千,所以迷宫可能很大很大,为了节省时间,在循环的时候就把“J”和“F”记录下来,因为题目中说J确定只有一个,而 没有说F的位置只有一个,所以就用数组记录F,普通变量记录J,先初始化F的着火时间,这里把所有的初始着火点全部压入初始队列,并把时间记录下来,然后再用BFS搜索人跑出来了时间即到最短出口的时间+1 ,人不能走记录时间小于当前时间的fire[x]y[对应的maze[x][y]的地方。

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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE

Malcolm Sharpe, Ondřej Lhoták



#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>

using namespace std;

int r,c;
char maze[1005][1005];
int d1[1005][1005];
int d2[1005][1005];
int vis[1005][1005];
int fx[1005], fy[1005];
int mov[4][2] = {
                0,1,
                0,-1,
                1,0,
                -1,0
                    };
typedef pair<int , int> P;

void bfs_fire(int cnt)
{
    queue<P> quef;
    memset(vis, 0, sizeof(vis));
    memset(d1, 1000010, sizeof(d1));
    for(int i = 0 ; i < cnt ; i++)
    {
        quef.push(P(fx[i], fy[i]));
        d1[fx[i]][fy[i]] = 0;
        vis[fx[i]][fy[i]] = 1;
    }
    while(quef.size())
    {
        P p = quef.front();
        quef.pop();
        for(int i = 0 ; i < 4 ; i++)
        {
            int nx = mov[i][0] + p.first;
            int ny = mov[i][1] + p.second;
            if(nx >= 0 && nx < r && ny >= 0 && ny < c && vis[nx][ny] == 0 && maze[nx][ny] == '.')
            {
                quef.push(P(nx, ny));
                vis[nx][ny] = 1;
                d1[nx][ny] = d1[p.first][p.second] + 1;
            }
        }
    }
    return;
}

int bfs_man(int sx, int sy)
{
    queue<P> que;
    memset(vis, 0, sizeof(vis));
    memset(d2, -1, sizeof(d2));
    que.push(P(sx, sy));
    d2[sx][sy] = 0;
    vis[sx][sy] = 1;
    while(que.size())
    {
        P p = que.front();
        que.pop();
        if(p.first == 0 || p.first == r-1 || p.second == 0 || p.second == c-1)
            return d2[p.first][p.second];
        for(int i = 0 ; i < 4 ; i++)
        {
            int nx = mov[i][0] + p.first;
            int ny = mov[i][1] + p.second;
            if(nx >= 0 && nx < r && ny >= 0 && ny < c && vis[nx][ny] == 0 && maze[nx][ny] == '.' && d2[p.first][p.second] + 1 < d1[nx][ny])
            {
                que.push(P(nx, ny));
                vis[nx][ny] = 1;
                d2[nx][ny] = d2[p.first][p.second] + 1;
            }
        }
    }
    return -1;
}

int main()
{
    int t;
    int sx, sy;
    scanf("%d", &t);
    while(t--)
    {
        int cnt = 0;
        scanf("%d%d", &r, &c);
        getchar();
        for(int i = 0 ; i < r ; i++)
        {
            for(int j = 0 ; j < c ; j++)
            {
                scanf("%c", &maze[i][j]);
                if( maze[i][j] == 'J')
                {
                    sx = i;
                    sy = j;
                }
                if(maze[i][j] == 'F')
                {
                    fx[cnt] = i;
                    fy[cnt] = j;
                    cnt++;
                }
            }
            getchar();
        }
        bfs_fire(cnt);
        int ans = bfs_man(sx ,sy);
        if(ans == -1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n", ans + 1);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值