hdu1180 诡异的楼梯 --bfs+优先级队列

遇到楼梯特殊处理

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

struct node {
    int x, y, t;
    node() {}
    node(int xx, int yy, int tt):x(xx), y(yy), t(tt) {}
    friend bool operator<(const node &a, const node &b) {
        return a.t > b.t;
    }
};

char mp[50][50];
bool vis[50][50];
int nx[] = {-1, 0, 1, 0};
int ny[] = {0, 1, 0, -1};

int bfs(int m, int n, int sx, int sy) {
    memset(vis, false, sizeof(vis));
    priority_queue<node> q;
    vis[sx][sy] = true;
    q.push(node(sx, sy, 0));
    while (!q.empty()) {
        node t = q.top();
        q.pop();
        if (mp[t.x][t.y] == 'T')
            return t.t;

        for (int i = 0; i < 4; i++) {
            int tx = t.x + nx[i];
            int ty = t.y + ny[i];
            int tt = t.t + 1;
            if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*')
                continue;
            if(mp[tx][ty] == '|' || mp[tx][ty] == '-') {
                if (mp[tx][ty] == '|') {
                    if ((i == 0 || i == 2) && t.t%2 || (i == 1 || i == 3) && t.t%2==0)
                        tt++;
                } else if (mp[tx][ty] == '-') {
                    if ((i == 0 || i == 2) && t.t%2==0 || (i == 1 || i == 3) && t.t%2)
                        tt++;
                }
                tx += nx[i];
                ty += ny[i];
                if (tx < 1 || tx > m || ty < 1 || ty > n || vis[tx][ty] || mp[tx][ty] == '*')
                    continue;
            }
            q.push(node(tx, ty, tt));
            vis[tx][ty] = true;
        }
    }
    return -1;
}

int main() {
    //freopen("in", "r", stdin);
    int m, n;
    while(~scanf("%d%d", &m, &n)) {
        int sx, sy;
        getchar();
        for (int i = 1; i <= m; i++) 
            fgets(mp[i]+1, 50, stdin);

        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                if (mp[i][j] == 'S') {
                    sx = i;
                    sy = j;
                    break;
                }

        printf("%d\n", bfs(m, n, sx, sy));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值