結果

問題 No.1323 うしらずSwap
ユーザー t33ft33f
提出日時 2020-12-20 11:11:31
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,508 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 134,196 KB
実行使用メモリ 47,676 KB
最終ジャッジ日時 2024-09-21 11:38:32
合計ジャッジ時間 9,513 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
6,940 KB
testcase_11 WA -
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 66 ms
33,600 KB
testcase_17 AC 98 ms
33,676 KB
testcase_18 AC 69 ms
33,644 KB
testcase_19 AC 102 ms
33,740 KB
testcase_20 AC 65 ms
33,548 KB
testcase_21 AC 92 ms
47,536 KB
testcase_22 AC 126 ms
47,536 KB
testcase_23 AC 90 ms
47,412 KB
testcase_24 AC 124 ms
47,416 KB
testcase_25 AC 91 ms
47,532 KB
testcase_26 AC 84 ms
47,560 KB
testcase_27 AC 81 ms
47,624 KB
testcase_28 AC 151 ms
33,632 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 274 ms
33,492 KB
testcase_34 AC 305 ms
33,604 KB
testcase_35 AC 300 ms
33,616 KB
testcase_36 AC 300 ms
33,608 KB
testcase_37 AC 301 ms
33,596 KB
testcase_38 AC 318 ms
33,456 KB
testcase_39 AC 313 ms
33,548 KB
testcase_40 AC 314 ms
33,620 KB
testcase_41 AC 311 ms
33,620 KB
testcase_42 AC 297 ms
33,624 KB
testcase_43 AC 79 ms
33,760 KB
testcase_44 AC 106 ms
33,504 KB
testcase_45 AC 112 ms
33,612 KB
testcase_46 AC 107 ms
33,596 KB
testcase_47 AC 94 ms
33,648 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 2 ms
6,944 KB
testcase_60 AC 2 ms
6,944 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstring>
#include <tuple>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
template<typename Callback>
inline void for_nbd4(int x, int y, int lx, int ux, int ly, int uy, Callback f) {
    constexpr pair<int, int> dxdy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    for (auto [dx, dy] : dxdy) {
        const int u = x + dx, v = y + dy;
        if (lx <= u && u < ux && ly <= v && v < uy) f(u, v);
    }
}
template<typename Callback>
inline void for_nbd4(int x, int y, int X, int Y, Callback f) {
    for_nbd4(x, y, 0, X, 0, Y, f);
}

template<typename T, typename Callback>
inline T sum_nbd4(int x, int y, int lx, int ux, int ly, int uy, Callback f) {
    constexpr pair<int, int> dxdy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    T result{};
    for (auto [dx, dy] : dxdy) {
        const int u = x + dx, v = y + dy;
        if (lx <= u && u < ux && ly <= v && v < uy) result += f(u, v);
    }
    return result;
}
template<typename T, typename Callback>
inline T sum_nbd4(int x, int y, int X, int Y, Callback f) {
    return sum_nbd4<T, Callback>(x, y, 0, X, 0, Y, f);
}

int main() {
    int h, w, sx, sy, gx, gy; cin >> h >> w >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;
    vector<string> S(h);
    for (int i = 0; i < h; i++) cin >> S[i];
    int dist[h][w], count[h][w];
    pair<int, int> from[h][w];
    constexpr int INF = 1<<30;
    priority_queue<tuple<int, int, int> > pq;
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
            if (i == sx && j == sy) {
                dist[i][j] = 0;
                count[i][j] = 1;
                from[i][j] = {-1, -1};
                pq.push({0, i, j});
            } else {
                dist[i][j] = INF;
                count[i][j] = 0;
            }
    while (!pq.empty()) {
        auto [d, x, y] = pq.top();
        pq.pop();
        d = -d;
        if (dist[x][y] < d) continue;
        for_nbd4(x, y, h, w, [d=d, x=x, y=y, &S, &dist, &pq, &count, &from](int u, int v) {
                if (S[u][v] == '#') return;
                if (dist[u][v] > dist[x][y] + 1) {
                    dist[u][v] = dist[x][y] + 1;
                    count[u][v] = count[x][y];
                    from[u][v] = {x, y};
                    pq.push({-dist[u][v], u, v});
                } else if (dist[u][v] == dist[x][y] + 1) {
                    // count[u][v] += count[x][y];
                    count[u][v] = min(count[u][v] + count[x][y], 2);
                }
            });
    }
    // for (int i = 0; i < h; i++)
    //     for (int j = 0; j < w; j++)
    //         cerr << i << ' ' << j << ' ' << dist[i][j] << ' ' << count[i][j] << endl;
    if (count[gx][gy] == 0) cout << -1 << endl;
    else if (count[gx][gy] > 1) cout << 2 * dist[gx][gy] << endl;
    else {
        for (int x = gx, y = gy;;) {
            tie(x, y) = from[x][y];
            if (x == sx && y == sy) break;
            if (sum_nbd4<int>(x, y, h, w, [&](int u, int v) {return int(S[u][v] == '.');}) > 2)
            {
                cout << 2 * dist[gx][gy] + 2 << endl;
                return 0;
            }
        }

        for (int x = gx, y = gy;;) {
            tie(x, y) = from[x][y];
            if (x == sx && y == sy) break;
            S[x][y] = '#';
        }

        const int d0 = dist[gx][gy];

        int dist[2][h][w];
        for (int k = 0; k < 2; k++) 
            for (int i = 0; i < h; i++)
                for (int j = 0; j < w; j++)
                    dist[k][i][j] = (k == 0 && i == sx && j == sy) ? 0 : INF;
        priority_queue<tuple<int, int, int, int> > pq;
        pq.push({0, 0, sx, sy});
        while (!pq.empty()) {
            auto [d, f, x, y] = pq.top();
            pq.pop();
            d = -d;
            if (dist[f][x][y] < d) continue;
            for_nbd4(x, y, h, w, [d=d, f=f, x=x, y=y, sx, sy, gx, gy, &S, &dist, &pq](int u, int v) {
                    if (S[u][v] == '#') return;
                    if (f == 1 && u == sx && v == sy) return;
                    if (f == 0 && u == gx && v == gy) return;
                    const int k = f == 1 ? 1 : (u == sx && v == sy) ? 0 : 1;
                    if (dist[k][u][v] > dist[f][x][y] + 1) {
                        dist[k][u][v] = dist[f][x][y] + 1;
                        pq.push({-dist[k][u][v], k, u, v});
                    }
                });
        }

        if (dist[1][gx][gy] == INF) cout << "-1" << endl;
        else cout << d0 + dist[1][gx][gy] << endl;
    }
}
0