UVA 11627 Fire!

題目鏈接

Description

Joe works in a maze. Unfortunately, portions of the maze havecaught on re, and the owner of the maze neglected to create a reescape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the mazeare on re, you must determine whether Joe can exit the maze beforethe re reaches him, and how fast he can do it.Joe and the re each move one square per minute, vertically orhorizontally (not diagonally). The re spreads all four directionsfrom each square that is on re. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the remay enter a square that is occupied by a wall.

Input

The rst line of input contains a single integer, the number of testcases to follow. The rst line of each test case contains the twointegersRandC, separated by spaces, with 1R;C1000. ThefollowingRlines of the test case each contain one row of the maze. Each of these lines contains exactlyCcharacters, 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 reThere will be exactly oneJin each test case.

Output

For each test case, output a single line containing `IMPOSSIBLE’ if Joe cannot exit the maze before the re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input
在這裏插入圖片描述
Sample Output
3
IMPOSSIBLE

大致題意
#s是牆,逗號是空地,F是火,J是起點,人開始跑火開始燒,問人是否可以成功脫逃

具體思路
就是一個基本的bfs題目,人優先火運動,因爲之前比賽遇到過一題類似的,但是那題存在多個火苗,巨毒瘤,瘋狂WA,一看題目 have fires,看到s的瞬間差點暴斃,那次之後遇到此類題目都是用雙端隊列存取全部火苗進行bfs廣搜遍歷

#include<stdio.h>
#include<deque>
#include<iostream>
using namespace std;
typedef long long int LLD;
LLD changex[5]= {1,-1,0,0};
LLD changey[5]= {0,0,-1,1};
char import[1005][1005];
LLD times[1005][1005];
int main()
{
    LLD t;
    scanf("%lld",&t);
    while (t--)
    {
        LLD r,c;
        scanf("%lld %lld%*c",&r,&c);
        fill(times[0],times[0]+1005*1005,1000005);
        deque<LLD>duix;
        deque<LLD>duiy;
        while (!duix.empty()||!duiy.empty())
        {
            duix.pop_front();
            duiy.pop_front();
        }
        for (LLD i=0; i<r; i++)
        {
            scanf("%s%*c",&import[i]);
            for (LLD j=0; j<c; j++)
            {
                if (import[i][j]=='J')
                {
                    duix.push_front(i);
                    duiy.push_front(j);
                    times[i][j]=0;
                }
                else if (import[i][j]=='F')
                {
                    duix.push_back(i);
                    duiy.push_back(j);
                }
            }
        }
        LLD flag=0;
        while (!duix.empty()||!duiy.empty())
        {
            LLD x=duix.front();
            LLD y=duiy.front();
            duix.pop_front();
            duiy.pop_front();
            if (import[x][y]!='F'&&(x+1>=r||y+1>=c||x-1<0||y-1<0))
            {
                printf("%lld\n",times[x][y]+1);
                flag=1;
                break;
            }
            if (import[x][y]=='F')
            {
                for (LLD i=0; i<4; i++)
                {
                    LLD xx=x+changex[i];
                    LLD yy=y+changey[i];
                    if (xx>=0&&xx<r&&yy>=0&&yy<c&&import[xx][yy]!='#'&&import[xx][yy]!='F')
                    {
                        import[xx][yy]='F';
                        duix.push_back(xx);
                        duiy.push_back(yy);
                    }
                }
            }
            else
            {
                for (LLD i=0; i<4; i++)
                {
                    LLD xx=x+changex[i];
                    LLD yy=y+changey[i];
                    if (xx>=0&&xx<r&&yy>=0&&yy<c&&
                            import[xx][yy]!='#'&&import[xx][yy]!='F'&&
                            times[xx][yy]>times[x][y]+1)
                    {
                        times[xx][yy]=times[x][y]+1;
                        duix.push_back(xx);
                        duiy.push_back(yy);
                    }
                }
            }
        }
        if (flag==0)
        {
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章