結果

問題 No.1323 うしらずSwap
ユーザー t33ft33f
提出日時 2020-12-20 13:53:22
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 3,000 ms
コード長 7,961 bytes
コンパイル時間 3,878 ms
コンパイル使用メモリ 134,240 KB
実行使用メモリ 35,580 KB
最終ジャッジ日時 2024-11-15 09:38:12
合計ジャッジ時間 10,383 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 65 ms
35,456 KB
testcase_17 AC 100 ms
35,456 KB
testcase_18 AC 65 ms
35,488 KB
testcase_19 AC 106 ms
35,328 KB
testcase_20 AC 65 ms
35,456 KB
testcase_21 AC 106 ms
35,456 KB
testcase_22 AC 177 ms
35,400 KB
testcase_23 AC 105 ms
35,456 KB
testcase_24 AC 167 ms
35,456 KB
testcase_25 AC 106 ms
35,328 KB
testcase_26 AC 100 ms
35,364 KB
testcase_27 AC 102 ms
35,456 KB
testcase_28 AC 81 ms
35,352 KB
testcase_29 AC 93 ms
35,368 KB
testcase_30 AC 168 ms
35,460 KB
testcase_31 AC 96 ms
35,396 KB
testcase_32 AC 169 ms
35,580 KB
testcase_33 AC 130 ms
35,200 KB
testcase_34 AC 126 ms
35,328 KB
testcase_35 AC 124 ms
35,488 KB
testcase_36 AC 121 ms
35,388 KB
testcase_37 AC 119 ms
35,504 KB
testcase_38 AC 116 ms
35,260 KB
testcase_39 AC 119 ms
35,328 KB
testcase_40 AC 123 ms
35,384 KB
testcase_41 AC 122 ms
35,464 KB
testcase_42 AC 114 ms
35,328 KB
testcase_43 AC 59 ms
35,456 KB
testcase_44 AC 78 ms
35,356 KB
testcase_45 AC 69 ms
35,548 KB
testcase_46 AC 76 ms
35,388 KB
testcase_47 AC 62 ms
35,516 KB
testcase_48 AC 68 ms
35,388 KB
testcase_49 AC 100 ms
35,348 KB
testcase_50 AC 66 ms
35,268 KB
testcase_51 AC 53 ms
35,344 KB
testcase_52 AC 59 ms
35,308 KB
testcase_53 AC 93 ms
35,488 KB
testcase_54 AC 58 ms
35,548 KB
testcase_55 AC 101 ms
35,276 KB
testcase_56 AC 57 ms
35,360 KB
testcase_57 AC 109 ms
35,456 KB
testcase_58 AC 2 ms
5,248 KB
testcase_59 AC 3 ms
5,248 KB
testcase_60 AC 2 ms
5,248 KB
testcase_61 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

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);
}

constexpr int INF = 1<<30;

int solve(vector<string> S, int sx, int sy, int gx, int gy) {
    const int h = S.size(), w = S.front().size();
    int dist[h][w], count[h][w];
    pair<int, int> from[h][w];
    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) return -1;
    else if (count[gx][gy] > 1) return 2 * dist[gx][gy];
    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) {
                return 2 * dist[gx][gy] + 2;
            }
        }

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

        for (int i = 0; i < h; i++) fill(dist[i], dist[i] + w, INF);
        q.emplace(sx, sy);
        q.emplace(gx, gy);
        dist[sx][sy] = 0;
        dist[gx][gy] = 0;
        // cerr << string(10, '-') << endl;
        while (!q.empty()) {
            auto [x, y] = q.front();
            // cerr << x << ' ' << y << endl;
            q.pop();
            if (sum_nbd4<int>(x, y, h, w, is_empty) > 2) {
                const int d1 = dist[x][y];
                ans = min(ans, 2 * d0 + 4 * (d1 + 1));
                break;
            }
            for_nbd4(x, y, h, w, [x=x, y=y, &dist, &q, &S](int u, int v) {
                    if (S[u][v] == '.' && dist[u][v] == INF) {
                        dist[u][v] = dist[x][y] + 1;
                        q.emplace(u, v);
                    }
                });
        }

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

        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]);
        return (ans == INF ? -1 : ans);
    }
}

int dp(const vector<string>& S, int sx, int sy, int gx, int gy) {
    const int h = S.size(), w = S.front().size();
    int dp[h][w][h][w];
    for (int a = 0; a < h; a++)
        for (int b = 0; b < w; b++)
            for (int c = 0; c < h; c++)
                for (int d = 0; d < w; d++)
                    dp[a][b][c][d] = INF;
    queue<tuple<int, int, int, int> > q;
    dp[sx][sy][gx][gy] = 0;
    
    q.emplace(sx, sy, gx, gy);
    while (!q.empty()) {
        auto [a, b, c, d] = q.front();
        q.pop();
        const int dist = dp[a][b][c][d];
        for_nbd4(a, b, h, w, [c=c, d=d, dist, &dp, &S, &q](int u, int v) {
                if (S[u][v] == '#') return;
                if (c == u && d == v) return;
                if (dp[u][v][c][d] > dist + 1) {
                    dp[u][v][c][d] = dist + 1;
                    q.emplace(u, v, c, d);
                }
            });
        for_nbd4(c, d, h, w, [a=a, b=b, dist, &dp, &S, &q](int u, int v) {
                if (S[u][v] == '#') return;
                if (a == u && b == v) return;
                if (dp[a][b][u][v] > dist + 1) {
                    dp[a][b][u][v] = dist + 1;
                    q.emplace(a, b, u, v);
                }
            });
    }
    return dp[gx][gy][sx][sy] == INF ? -1 : dp[gx][gy][sx][sy];
}

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];
    cout << solve(S, sx, sy, gx, gy) << endl;

    // int h = 6, w = 6;
    // vector S(h, string(w, '#'));
    // const int k = (h - 2) * (w - 2);
    // for (int s = 0; s < 1<<k; s++) {
    //     if ((s & 1023) == 0) cerr << "s = " << s << " of " << (1<<k) << endl;
    //     for (int idx = 0, i = 1; i < h-1; i++)
    //         for (int j = 1; j < w-1; j++) {
    //             S[i][j] = (((s >> idx) & 1) == 1 ? '#' : '.');
    //             idx++;
    //             assert(idx <= k);
    //         }
    //     for (int a = 1; a < h-1; a++)
    //         for (int b = 1; b < w-1; b++) {
    //             if (S[a][b] == '#') continue;
    //             for (int c = 1; c < h-1; c++)
    //                 for (int d = 1; d < w-1; d++) {
    //                     if (S[c][d] == '#') continue;
    //                     if (a == c && b == d) continue;
    //                     int ans = solve(S, a, b, c, d),
    //                         ans2 = dp(S, a, b, c, d);
    //                     if(ans != ans2) {
    //                         cerr << a << ' ' << b << ' ' << c << ' ' << d << endl;
    //                         S[a][b] = 'A';
    //                         S[c][d] = 'B';
    //                         for (string &line : S) cerr << line << endl;
    //                         cout << " ==> " << ans << ' ' << ans2 << endl;
    //                         S[a][b] = '.';
    //                         S[c][d] = '.';
    //                         return 0;
    //                     }
    //                 }
    //         }
    // }
}
0