C - Fire! UVA - 11624

2-Day2-C
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,着火的广场

问能否走出,输出需要的时间否则输出possible

思路:BFS,确定火走到每个点最小步数,再bfs一次joe的路径,如果当前步数小于火烧到这个位置,那么可走

代码如下:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

int n,m;
int mp[1005][1005];
int fire[1005][1005];
int vis[1005][1005];
int dir[4][2]={0,1,0,-1,1,0,-1,0};

struct node
{
    int x,y;
    int step;
}s,f;

queue<node>Q;

bool check(int x,int y)
{
    if(x<=0||x>n||y<=0||y>m||vis[x][y]==1||mp[x][y]=='#')
        return false;
    return true;
}

void bfs1()
{

    node now,next;
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.step=now.step+1;
            if(check(next.x,next.y))
            {
                vis[next.x][next.y]=1;
                fire[next.x][next.y]=next.step;
                Q.push(next);
            }
        }
    }
}

int bfs()
{
    MEM(vis,0);  
    queue<node>q;
    node now,next;
    now.x=s.x;
    now.y=s.y;
    now.step=1;
    vis[now.x][now.y]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==1||now.x==n||now.y==1||now.y==m)
            return now.step;
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.step=now.step+1;
            if(check(next.x,next.y)&&next.step<fire[next.x][next.y])
            {
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //std::ios::sync_with_stdio(false);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        MEM(fire,INF);  
        MEM(vis,0);     
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf(" %c",&mp[i][j]);
                if(mp[i][j]=='J')
                {
                    s.x=i;
                    s.y=j;
                }
                if(mp[i][j]=='F')
                {
                    node now;
                    now.x=i;
                    now.y=j;
                    now.step=1;
                    vis[i][j]=1;
                    fire[i][j]=1;
                    Q.push(now);
                }
            }
        }
        bfs1(); 
        int ans = bfs();  
        if(ans==-1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务