估计写出ub了,但是找不到在哪...(马蜂优越,思路清晰,玄关)
查看原帖
估计写出ub了,但是找不到在哪...(马蜂优越,思路清晰,玄关)
22716
神仙酱楼主2024/3/8 20:28

本地测试和测评机输出不同,对于样例,我的答案是正确的,但是测评机输出是5...

#include <bits/stdc++.h>
using namespace std;
const int N = 60;
bool mp[N][N];
int cost[N][N][4];
int n, m;
inline int ori2idx(char ch) {
    if (ch == 'N')
        return 0;
    if (ch == 'E')
        return 1;
    if (ch == 'S')
        return 2;
    return 3;
}

struct state {
    state(int _t, int _x, int _y, int _ori) : t(_t), x(_x), y(_y), ori(_ori) {
    }
    int t, x, y;
    char ori;
    bool operator>(const state& s) const {
        return t > s.t;
    }
};
bool check(int x, int y) {
    if (x < 0 || y < 0)
        return false;
    if (x >= n || y >= m)
        return false;
    return true;
}
void cond_assign(int& dst, int v) {
    if (dst == -1)
        dst = v;
}
int sx, sy, ex, ey;
int dx[] = { -1, 0, 1, 0 }, dx1[] = { -1, 0, 0, 0 }, dx2[] = { -1, -1, 0, -1 };
int dy[] = { 0, 1, 0, -1 }, dy1[] = { 0, 0, 0 - 1 }, dy2[] = { -1, 0, -1, -1 };
char ori;
int bfs() {
    priority_queue<state, vector<state>, greater<state>> q;
    cost[sx][sy][ori2idx(ori)] = 0;
    q.emplace(0, sx, sy, ori2idx(ori));
    while (q.size()) {
        auto top = q.top();
        q.pop();
        int path[] = { ori, (top.ori + 1) & 3, (top.ori + 3) & 3, (top.ori + 2) & 3 };
        cond_assign(cost[top.x][top.y][path[1]], cost[top.x][top.y][top.ori] + 1);
        cond_assign(cost[top.x][top.y][path[2]], cost[top.x][top.y][top.ori] + 1);
        cond_assign(cost[top.x][top.y][path[3]], cost[top.x][top.y][top.ori] + 2);

        for (int i : path)
            for (int j = 1; j <= 3; ++j) {
                int x = top.x + dx[i] * j, y = top.y + dy[i] * j;
                if (!check(x, y))
                    break;
                if (check(x + dx1[i], y + dy1[i]) && mp[x + dx1[i]][y + dy1[i]])
                    break;
                if (check(x + dx2[i], y + dy2[i]) && mp[x + dx2[i]][y + dy2[i]])
                    break;
                if (cost[x][y][i] != -1)
                    continue;
                cost[x][y][i] = cost[top.x][top.y][i] + 1;
                if (x == ex && y == ey)
                    return cost[x][y][i];
                q.emplace(cost[x][y][i], x, y, i);
            }
    }
    return -1;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    memset(cost, -1, sizeof cost);
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j)
            cin >> mp[i][j];
    }

    cin >> sx >> sy >> ex >> ey;
    cin >> ori;
    if (sx == ex && sy == ey) {
        cout << 0;
        return 0;
    }
    cout << bfs();
}
2024/3/8 20:28
加载中...