結果

問題 No.1323 うしらずSwap
ユーザー t33ft33f
提出日時 2020-12-20 12:03:18
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,361 bytes
コンパイル時間 2,115 ms
コンパイル使用メモリ 122,588 KB
実行使用メモリ 33,764 KB
最終ジャッジ日時 2023-10-21 10:34:13
合計ジャッジ時間 7,370 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 66 ms
33,696 KB
testcase_17 AC 97 ms
33,656 KB
testcase_18 AC 61 ms
33,720 KB
testcase_19 AC 96 ms
33,660 KB
testcase_20 AC 63 ms
33,676 KB
testcase_21 AC 80 ms
33,676 KB
testcase_22 AC 125 ms
33,696 KB
testcase_23 AC 81 ms
33,652 KB
testcase_24 AC 123 ms
33,684 KB
testcase_25 AC 81 ms
33,652 KB
testcase_26 AC 75 ms
33,720 KB
testcase_27 AC 76 ms
33,720 KB
testcase_28 AC 79 ms
33,684 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 131 ms
33,488 KB
testcase_34 AC 128 ms
33,580 KB
testcase_35 AC 126 ms
33,644 KB
testcase_36 AC 125 ms
33,732 KB
testcase_37 AC 121 ms
33,644 KB
testcase_38 AC 119 ms
33,492 KB
testcase_39 AC 122 ms
33,576 KB
testcase_40 AC 123 ms
33,648 KB
testcase_41 AC 127 ms
33,736 KB
testcase_42 AC 119 ms
33,648 KB
testcase_43 AC 56 ms
33,700 KB
testcase_44 AC 73 ms
33,616 KB
testcase_45 AC 66 ms
33,764 KB
testcase_46 AC 74 ms
33,616 KB
testcase_47 AC 60 ms
33,680 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
4,348 KB
testcase_60 AC 2 ms
4,348 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#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;
    queue<tuple<int, int> > q;
    q.emplace(sx, sy);
    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};
            } else {
                dist[i][j] = INF;
                count[i][j] = 0;
            }
    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();
        for_nbd4(x, y, h, w, [x=x, y=y, &S, &dist, &q, &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};
                    q.emplace(u, v);
                } else if (dist[u][v] == dist[x][y] + 1) {
                    assert(count[u][v] > 0);
                    assert(count[x][y] > 0);
                    count[u][v] = 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;
    auto is_empty = [&S](int u, int v) -> int {return int(S[u][v] == '.');};

    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, is_empty) > 2) {
                cout << 2 * dist[gx][gy] + 2 << endl;
                return 0;
            }
        }

        int ans = INF;
        if (sum_nbd4<int>(gx, gy, h, w, is_empty) == 4
            || sum_nbd4<int>(sx, sy, h, w, is_empty) == 4) {
            ans = 2 * dist[gx][gy] + 4;
        }

        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];

        for (int i = 0; i < h; i++) fill(dist[i], dist[i] + w, INF);
        for_nbd4(sx, sy, h, w, [gx, gy, &q, &dist, &S](int u, int v) {
                if (S[u][v] == '.' && (u != gx || v != gy)) {
                    q.emplace(u, v);
                    dist[u][v] = 1;
                }
            });
        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            for_nbd4(x, y, h, w, [x=x, y=y, sx, sy, gx, gy, &S, &dist, &q](int u, int v) {
                    if (S[u][v] == '#') return;
                    if (u == sx && v == sy) return;
                    if (dist[u][v] > dist[x][y] + 1) {
                        dist[u][v] = dist[x][y] + 1;
                        q.emplace(u, v);
                    }
                });
        }

        ans = min(ans, d0 + dist[gx][gy]);
        cout << (ans == INF ? -1 : ans) << endl;
    }
}
0