UVA 11624——Fire!

3 篇文章 0 订阅

题目链接:https://odzkskevi.qnssl.com/f08168bf425521a08eac790e3a9ae7e3?v=1501661684

题目解析:BFS应用。先令每个Fire(Fire不唯一)跑一遍BFS,用一个data数组记录下每个点(不包括墙)被Fire到得最短时间,然后再令J跑BFS,在判定的时候增加一个判断是否能在Fire到达之前走的条件。

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <climits>
using namespace std;
const int MAXN=1e3+10;
int data[MAXN][MAXN];///记录Fire到的时间
char sun[MAXN][MAXN];///字符储存数组
bool pin[MAXN][MAXN];///标记数组
int R, C;
int sx, sy;
int ex[MAXN], ey[MAXN];
int sum;///记录Fire的个数
int stepx[4]= {1,-1,0,0};
int stepy[4]= {0,0,1,-1};
struct node
{
    int numx, numy, step;
};
bool zhao(int a, int b)
{
    if(a>=0&&a<R&&b>=0&&b<C)
        return true;
    else
        return false;
}
int BFS()///J的BFS
{
    queue<struct node>Q;
    struct node A, B;
    A=(node)
    {
        sx,sy,0
    };
    Q.push(A);
    pin[sx][sy]=true;
    while(!Q.empty())
    {
        A=Q.front();
        Q.pop();
        for(int i=0; i<4; i++)
        {
            int c=A.numx+stepx[i];
            int d=A.numy+stepy[i];
            if(!zhao(c,d))///如果已经出界,直接结束
                return A.step+1;
            if(zhao(c,d)&&!pin[c][d]&&sun[c][d]!='#'&&A.step+1<data[c][d])///与Fire到达的时间比较
            {
                B=(node)
                {
                    c,d,A.step+1
                };
                Q.push(B);
                pin[c][d]=true;
            }
        }
    }
    return -1;
}
void DFS(int a, int b, int c)///FireBFS函数,可以不加pin标记数组,加上缩短时间
{
    struct node A, B;
    queue<struct node>P;
    A=(node)
    {
        a,b,c
    };
    P.push(A);
    data[a][b]=c;
    while(!P.empty())
    {
        A=P.front();
        P.pop();
        for(int i=0; i<4; i++)
        {
            int c=A.numx+stepx[i];
            int d=A.numy+stepy[i];
            if(zhao(c,d)&&sun[c][d]!='#'&&(A.step+1<data[c][d]))
            {
                B=(node)
                {
                    c,d,A.step+1
                };
                data[c][d]=A.step+1;
                P.push(B);
            }
        }
    }
}
void start()
{
    for(int i=0; i<R; i++)
    {
        for(int j=0; j<C; j++)
            data[i][j]=INT_MAX;///在climits函数中
    }
}
int main()
{
    int T;
    while(cin>>T)
    {
        while(T--)
        {
            cin>>R>>C;
            sum=0;
            start();
            for(int i=0; i<R; i++)
            {
                for(int j=0; j<C; j++)
                {
                    cin>>sun[i][j];
                    if(sun[i][j]=='J')
                    {
                        sx=i;
                        sy=j;
                    }
                    else if(sun[i][j]=='F')
                    {
                        ex[sum]=i;
                        ey[sum++]=j;///记录Fire,也可以存在队列中,在跑BFS时同时跑所有的Fire
                    }
                }
            }
            for(int i=0; i<sum; i++)
            {
                DFS(ex[i],ey[i],0);
            }
            memset(pin,false,sizeof(pin));
            int t=BFS();
            if(t==-1)
                cout<<"IMPOSSIBLE"<<endl;
            else
                cout<<t<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值