UVA 11624 Fire!(两次BFS、超级源)

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

题目大意:给你一个R*C的迷宫,“.”表示空地,“#”表示墙,“J”表示你现在的初始位置,“F”表示货火源(可能有若干个),现在要走出迷宫。火源每S能向四个方向延生一格,人每S能往四个方向走一格,人不能碰到火,问你走出迷宫的最少时间。如果不能就,就输出“IMPOSSIBLE”。

思路:简单的搜索题。不难想到,确定了火源之后,每个点的最早着火时间就已经确定了,火着了之后不会熄灭。然后在以人为源点BFS即可。每个点的最早着火时间,就是求有多个起点时的最短路。可以将所有火源都加入到初始队列(超级源的概念),然后BFS。等距离搜,搜到肯定就是最短的。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int MAX_R = 1111;
const int MAX_C = 1111;
const int INF = 0x0fffffff;

char Map[MAX_R][MAX_C];

int Map_t[MAX_R][MAX_C];

struct Node
{
    int x,y,t;
    Node(int xx,int yy,int tt)
    {
        x = xx;y = yy;t = tt;
    }
};

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

queue <Node> q;

int vis[MAX_R][MAX_C];

void init(int r,int c)
{
    while(!q.empty())
    {
        q.pop();
    }
    for(int i = 0;i < r;i++)
    {
        for(int j = 0;j < c;j++)
        {
            Map_t[i][j] = INF;
            if(Map[i][j] == 'F')
            {
                q.push(Node(i,j,0));
                Map_t[i][j] = 0;
            }
        }
    }
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
        Node cur = q.front();
        q.pop();
        for(int dd = 0;dd < 4;dd ++)
        {
            int xx = cur.x+dir[0][dd];
            int yy = cur.y+dir[1][dd];
            if(xx >= 0 && yy >= 0 && xx < r && yy < c && Map[xx][yy] == '.' && !vis[xx][yy])
            {
                vis[xx][yy] = 1;
                Map_t[xx][yy] = cur.t+1;
                q.push(Node(xx,yy,cur.t+1));
            }
        }
    }
}

int slove(int r,int c)
{
    while(!q.empty()) q.pop();
    int ok = 0;
    memset(vis,0,sizeof(vis));
    for(int i = 0;i < r && !ok;i++)
        for(int j = 0;j < c && !ok;j++)
            if(Map[i][j] == 'J')
            {
                ok = 1;
                q.push(Node(i,j,0));
                vis[i][j] = 1;
                break;
            }
    int ans = 0;
    ok = 0;
    while(!q.empty())
    {
        Node cur = q.front();
        q.pop();
        for(int dd = 0;dd < 4;dd ++)
        {
            int xx = cur.x+dir[0][dd];
            int yy = cur.y+dir[1][dd];
            if(xx < 0 || yy < 0 || xx >= r || yy >= c)
            {
                ans = cur.t+1;
                ok = 1;
                break;
            }
            if(!vis[xx][yy] && Map[xx][yy] == '.' && Map_t[xx][yy] > cur.t+1)
            {
                vis[xx][yy] = 1;
                Node next(xx,yy,cur.t+1);
                q.push(next);
            }
        }
        if(ok) break;
    }
    return ans;
}

int main()
{
    int _;
    scanf("%d",&_);
    while(_--)
    {
        int r,c;
        scanf("%d%d",&r,&c);
        for(int i = 0;i < r;i++)
        {
            scanf("%s",Map[i]);
        }
        init(r,c);
        int ans = slove(r,c);
        if(ans)
        {
            printf("%d\n",ans);
        }
        else puts("IMPOSSIBLE");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值