ZOJ 1649 Rescue

//http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <map>
#define maxn 200 + 10
#define INF 0x7fffffff
using namespace std;
char g[maxn][maxn];
int stx, sty;
bool vis[maxn][maxn];
int move[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n, m;
struct POINT
{
    int cur;
    int x, y;
};
priority_queue<POINT> q;
bool operator<(POINT a, POINT b)
{
    return a.cur > b.cur;
}
int bfs()
{
    POINT tmp;
    memset(vis, false, sizeof(vis));
    tmp.x = stx;
    tmp.y = sty;
    tmp.cur = 0;
    vis[stx][sty] = true;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.top();
        q.pop();
        for(int i = 0; i < 4; ++i)
        {
            int dcur = tmp.cur;
            int dx = tmp.x + move[i][0];
            int dy = tmp.y + move[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !vis[dx][dy])
            {
                vis[dx][dy] = true;
                if(g[dx][dy] == 'a')
                    return dcur+1;
                else if(g[dx][dy] == '.')
                {
                    POINT t;
                    t.cur = dcur+1;
                    t.x = dx;
                    t.y = dy;
                    q.push(t);
                }
                else if(g[dx][dy] == 'x')
                {
                    POINT t;
                    t.cur = dcur+2;
                    t.x = dx;
                    t.y = dy;
                    q.push(t);
                }
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        while(!q.empty())
            q.pop();
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", g[i]);
            for(int j = 0; j < m; ++j)
                if(g[i][j] == 'r')
                    stx = i, sty = j;
        }
        int ans = bfs();
        if(ans == -1) puts("Poor ANGEL has to stay in the prison all his life.");
        else printf("%d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值