UVa -11624 Fire -bfs

又到了开始练习ACM的季节了。不知道这次会坚持多久。但是希望自己好好坚持吧,自己选的路要对自己负责。

UVA 11624FIRE


给出题目链接,题意大致是一张地图,有障碍,有几堆火焰会随着时间蔓延开来,Joe以同样的速度移动,问题就是Joe能不能安全跑出图外。

思路就是两遍bfs,第一遍先把所有的火焰都加入队列,bfs出各个点最早被火焰覆盖的时间;第二遍再简单地bfs人的步数,在火烧到之前能到达的格子。
自己写的代码里的一些bug:一开始就在边界的情况;火被墙挡了根本烧不到的情况。就这样。

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxN 1001

typedef struct Point{
    int x,y;
    Point(int xx=0,int yy =0): x(xx),y(yy){}
} point;

int r,c;
char map[maxN][maxN];
int f[maxN][maxN],fj[maxN][maxN];
point joe;

struct Queue{
    point que[maxN*maxN];
    int head,tail;
    void clear(){
        head=0;
        tail=0;
    }
    void append(int x, int y){
        tail++;
        que[tail].x=x;
        que[tail].y=y;
    }
    point pop(){
        return que[head];
    }
} q;

int dir[8]={0,1,0,-1,1,0,-1,0};

void bfs_fire(){
    while (q.head<q.tail){
        q.head++;
        point cur = q.pop();
        int xx,yy,ff;
        for (int i = 0; i<=3; ++i){
            xx=cur.x+dir[i];
            yy=cur.y+dir[i+4];
            ff=f[cur.x][cur.y];
            if (xx>0 && xx<=r && yy>0 && yy<=c && map[xx][yy]!='#' && f[xx][yy]==0){
                q.append(xx,yy);
                f[xx][yy]=ff+1;
            }
        }
    }
}

int bfs(){
    while (q.head<q.tail){
        q.head++;
        point cur = q.pop();
        int xx,yy,ff;
        for (int i = 0; i<=3; ++i){
            xx=cur.x+dir[i];
            yy=cur.y+dir[i+4];
            ff=fj[cur.x][cur.y];
            if (xx>0 && xx<=r && yy>0 && yy<=c && map[xx][yy]!='#' && (f[xx][yy]>ff+1 || f[xx][yy]==0) && fj[xx][yy]==0){
                if (xx==1 || yy==1 || xx==r || yy==c) {
                    return ff+1;
                }
                q.append(xx,yy);
                fj[xx][yy]=ff+1;
            }
        }
    }
    return 0;
}

int main(){
    int test;
    char ch;
    cin >> test;
    while (test--){
        cin >>r >> c;
        scanf("%c",&ch);  
        q.clear();
        for (int i=1; i<=r; ++i ){
            for (int j=1; j<=c; ++j){
                scanf("%c",&map[i][j]);
                f[i][j]=0;
                fj[i][j]=0;
                if (map[i][j]=='F') {
                    q.append(i,j);
                    f[i][j]=1;
                }
                else if (map[i][j]=='J'){
                    joe.x=i;
                    joe.y=j;
                    fj[i][j]=1;
                }
            }
            scanf("%c",&ch);
        }
        if (joe.x==1 || joe.y==1 || joe.x==r || joe.y==c) {
            cout<<1<<endl;
            continue;
        }

        bfs_fire();
        for (int i=1; i<=r; ++i) {
            for (int j=1; j<=c; ++j){
                cout<<f[i][j];
            }
            cout << endl;
        }
        q.clear();
        q.append(joe.x,joe.y);
        int ans = bfs();
        if (ans==0) {
            cout<<"IMPOSSIBLE"<<endl;
        }
        else cout<<ans<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值