记不住就发一下

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e3 + 7;
int n, m;
char mp[maxN][maxN];
bool vis[maxN][maxN][4];
struct node {
    int x, y, s, d;
    node(int a = 0, int b = 0, int c = 0, int _d = 0) {
        x = a; y = b; s = c; d = _d;
    }
};
queue<node> Q;
const int dir[4][2] = {
    -1, 0,
    0, -1,
    0, 1,
    1, 0
};
bool In_map(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= m; }
void bfs() {
    while(!Q.empty()) Q.pop();
    int sta = 0;
    if(mp[1][1] == '@') sta = 1;
    if(mp[1][1] == '#') sta = 2;
    Q.push(node(1, 1, sta, 0));
    vis[1][1][sta] = true;
    while(!Q.empty()) {
        node now = Q.front();
        Q.pop();
        int x = now.x, y = now.y;
        sta = now.s;
        if(sta == 3) {
            printf("%d\n", now.d);
            return;
        }
        for(int i = 0, xx, yy; i < 4; i ++) {
            xx = x + dir[i][0];
            yy = y + dir[i][1];
            sta = now.s;
            if(!In_map(xx, yy) || mp[xx][yy] == '*') continue;
            if(mp[xx][yy] == '@') sta |= 1;
            if(mp[xx][yy] == '#') sta |= 2;
            if(vis[xx][yy][sta]) continue;
            vis[xx][yy][sta] = true;
            Q.push(node(xx, yy, sta, now.d + 1));
        }
    }
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++) scanf("%s", mp[i] + 1);
    bfs();
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值