rescue 救救天使

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char map[202][202];
const int maxi = 0x7FFFFFFF;int n, m,Count; 
struct site {
    int l, r,time;
    friend bool operator<(site c1, site c2)
    {
        return c2.time < c1.time;
    }
};
int Move[4][2] = { {-1,0},{1,0},{0,-1},{0,1} },mintime[202][202];
bool in(int a, int b){
    return a > 0 && a <= n&& b > 0 && b <= m&&map[a][b]!='#'; }//判断点是否在范围内,同时不是墙壁
int main() {
    bool  judge;
    site b,c;
    while (cin >> n >> m) {
        judge=0;
        memset(mintime, 0, sizeof(mintime));
        for (int i =1; i <= n; i++) {
            for (int j = 1; j <= m; j++) { 
                cin >> map[i][j];
                if (map[i][j] == 'a') { b.l = i; b.r = j; }//终点b
                else if (map[i][j] == 'r') {
                    c.l = i; 
                    c.r = j; 
                    c.time = 0; 
                }//起点c
            }//处理输入信息 
        }
        priority_queue<site>a;
        site cur, next;
        a.push(c);
        while (!a.empty()) {
            cur = a.top();
            a.pop();
            if (cur.l == b.l && cur.r == b.r) {
                judge = 1;
                Count = cur.time;
            }
            for (int i = 0; i < 4; i++) {//移动的四个方向
                next.l = cur.l + Move[i][0];
                next.r = cur.r + Move[i][1];
                if (in(next.l, next.r)) {//判断能否到达
                    next.time = cur.time + 1;//无论如何都+1
                    if (map[next.l][next.r] == 'x') next.time++;//如果是守卫再加1
                    if (next.time <= mintime[next.l][next.r] || mintime[next.l][next.r] == 0) {
                        mintime[next.l][next.r] = next.time;
                        a.push(next);
                    }
                }
            }
        }
        if (judge)cout <<Count<< endl;
        else cout<<"Poor ANGEL has to stay in the prison all his life." << endl;
    }
}

做到了后面的题目才发现,好像可以直接标记点(x,y)有没有被访问过,毕竟是用了优先队列,先出来(判断能否满足条件的走法)肯定是时间在队列中是最小的走法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值