結果

問題 No.1323 うしらずSwap
ユーザー maine_honzukimaine_honzuki
提出日時 2020-12-20 01:11:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 3,000 ms
コード長 6,499 bytes
コンパイル時間 2,727 ms
コンパイル使用メモリ 219,812 KB
実行使用メモリ 19,840 KB
最終ジャッジ日時 2024-11-15 09:32:34
合計ジャッジ時間 8,849 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 2 ms
6,816 KB
testcase_15 AC 2 ms
6,816 KB
testcase_16 AC 65 ms
12,928 KB
testcase_17 AC 83 ms
12,672 KB
testcase_18 AC 63 ms
12,800 KB
testcase_19 AC 85 ms
12,800 KB
testcase_20 AC 64 ms
12,928 KB
testcase_21 AC 122 ms
19,840 KB
testcase_22 AC 162 ms
19,840 KB
testcase_23 AC 121 ms
19,712 KB
testcase_24 AC 162 ms
19,712 KB
testcase_25 AC 122 ms
19,712 KB
testcase_26 AC 99 ms
19,712 KB
testcase_27 AC 113 ms
19,840 KB
testcase_28 AC 68 ms
13,056 KB
testcase_29 AC 90 ms
19,840 KB
testcase_30 AC 109 ms
19,840 KB
testcase_31 AC 89 ms
19,840 KB
testcase_32 AC 108 ms
19,840 KB
testcase_33 AC 113 ms
13,056 KB
testcase_34 AC 106 ms
12,928 KB
testcase_35 AC 103 ms
13,184 KB
testcase_36 AC 102 ms
13,056 KB
testcase_37 AC 100 ms
13,056 KB
testcase_38 AC 98 ms
12,800 KB
testcase_39 AC 99 ms
12,928 KB
testcase_40 AC 98 ms
13,056 KB
testcase_41 AC 99 ms
13,056 KB
testcase_42 AC 97 ms
13,056 KB
testcase_43 AC 55 ms
13,056 KB
testcase_44 AC 64 ms
12,928 KB
testcase_45 AC 62 ms
13,184 KB
testcase_46 AC 66 ms
12,928 KB
testcase_47 AC 59 ms
13,056 KB
testcase_48 AC 73 ms
19,712 KB
testcase_49 AC 83 ms
19,840 KB
testcase_50 AC 70 ms
19,840 KB
testcase_51 AC 60 ms
19,712 KB
testcase_52 AC 64 ms
19,712 KB
testcase_53 AC 84 ms
19,712 KB
testcase_54 AC 64 ms
19,840 KB
testcase_55 AC 86 ms
19,712 KB
testcase_56 AC 62 ms
19,712 KB
testcase_57 AC 90 ms
19,840 KB
testcase_58 AC 2 ms
6,816 KB
testcase_59 AC 2 ms
6,820 KB
testcase_60 AC 2 ms
6,816 KB
testcase_61 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int H, W, ax, ay, bx, by;
vector<string> S;


int main() {
    cin >> H >> W >> ax >> ay >> bx >> by;
    ax--;
    ay--;
    bx--;
    by--;
    S.resize(H);
    for (int i = 0; i < H; i++) {
        cin >> S[i];
        for (auto& c : S[i])
            if (c == '#')
                c = 1;
            else
                c = 0;
    }

    const int dx[] = {1, 0, -1, 0};
    const int dy[] = {0, -1, 0, 1};

    queue<pair<int, pair<int, int>>> que;
    que.push(make_pair(0, make_pair(ax, ay)));
    vector<vector<int>> dist(H, vector<int>(W, -1));
    dist[ax][ay] = 0;
    que.push(make_pair(0, make_pair(ax, ay)));
    while (!que.empty()) {
        auto p = que.front();
        que.pop();
        int d = p.first, now_x = p.second.first, now_y = p.second.second;
        if (now_x == bx && now_y == by)
            continue;
        for (int i = 0; i < 4; i++) {
            int nxt_x = now_x + dx[i], nxt_y = now_y + dy[i];
            if (S[nxt_x][nxt_y] == 1) {
                continue;
            }
            if (dist[nxt_x][nxt_y] == -1) {
                dist[nxt_x][nxt_y] = d + 1;
                que.push(make_pair(d + 1, make_pair(nxt_x, nxt_y)));
            }
        }
    }

    if (dist[bx][by] == -1) {
        cout << -1 << endl;
        return 0;
    }

    bool flag1 = false, flag2 = false;
    int now_x = bx, now_y = by, now_dist = dist[bx][by];
    while (!(now_x == ax && now_y == ay)) {
        int cnt1 = 0, cnt2 = 0, tmp_x, tmp_y;
        for (int i = 0; i < 4; i++) {
            int nxt_x = now_x + dx[i], nxt_y = now_y + dy[i];
            if (dist[nxt_x][nxt_y] == now_dist - 1) {
                cnt1++;
                tmp_x = nxt_x;
                tmp_y = nxt_y;
            }
            if (S[nxt_x][nxt_y] == 0)
                cnt2++;
        }
        if (cnt1 >= 2)
            flag1 = true;
        if (now_dist != dist[bx][by])
            if (cnt2 >= 3)
                flag2 = true;

        now_x = tmp_x;
        now_y = tmp_y;
        now_dist--;
    }
    if (flag1) {
        cout << dist[bx][by] * 2 << endl;
    } else if (flag2) {
        cout << dist[bx][by] * 2 + 2 << endl;
    } else {
        int ans = 1e9;
        //case 3
        vector<int> d;
        for (int i = 0; i < 4; i++) {
            int tmp = dist[bx + dx[i]][by + dy[i]];
            if (tmp != -1)
                d.emplace_back(tmp);
        }
        sort(d.begin(), d.end());
        if (d.size() >= 2) {
            ans = min(ans, d[0] + d[1] + 2);
        }

        //case 1
        for (int x = 1; x < H - 1; x++) {
            for (int y = 1; y < W - 1; y++) {
                if (dist[x][y] == -1)
                    continue;
                int cnt = 0;
                for (int i = 0; i < 4; i++) {
                    int nxt_x = x + dx[i], nxt_y = y + dy[i];
                    if (dist[nxt_x][nxt_y] == -1)
                        continue;
                    cnt += (dist[nxt_x][nxt_y] == dist[x][y] + 1);
                }
                if (dist[x][y] >= 1 && cnt >= 2)
                    ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4 + 4);
                if (dist[x][y] == 0 && cnt >= 3)
                    ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4 + 4);
            }
        }

        //case 4
        for (int x = 1; x < H - 1; x++) {
            for (int y = 1; y < W - 1; y++) {
                if (dist[x][y] <= 0)
                    continue;
                int cnt = 0;
                for (int i = 0; i < 4; i++) {
                    int nxt_x = x + dx[i], nxt_y = y + dy[i];
                    if (dist[nxt_x][nxt_y] == -1)
                        continue;
                    cnt += (dist[nxt_x][nxt_y] == dist[x][y] - 1);
                }
                if (cnt >= 2)
                    ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4);
            }
        }


        //case 2
        {
            swap(ax, bx);
            swap(ay, by);
            queue<pair<int, pair<int, int>>> que;
            que.push(make_pair(0, make_pair(ax, ay)));
            vector<vector<int>> dist(H, vector<int>(W, -1));
            dist[ax][ay] = 0;
            que.push(make_pair(0, make_pair(ax, ay)));
            while (!que.empty()) {
                auto p = que.front();
                que.pop();
                int d = p.first, now_x = p.second.first, now_y = p.second.second;
                if (now_x == bx && now_y == by)
                    continue;
                for (int i = 0; i < 4; i++) {
                    int nxt_x = now_x + dx[i], nxt_y = now_y + dy[i];
                    if (S[nxt_x][nxt_y] == 1) {
                        continue;
                    }
                    if (dist[nxt_x][nxt_y] == -1) {
                        dist[nxt_x][nxt_y] = d + 1;
                        que.push(make_pair(d + 1, make_pair(nxt_x, nxt_y)));
                    }
                }
            }
            for (int x = 1; x < H - 1; x++) {
                for (int y = 1; y < W - 1; y++) {
                    if (dist[x][y] == -1)
                        continue;
                    int cnt = 0;
                    for (int i = 0; i < 4; i++) {
                        int nxt_x = x + dx[i], nxt_y = y + dy[i];
                        if (dist[nxt_x][nxt_y] == -1)
                            continue;
                        cnt += (dist[nxt_x][nxt_y] == dist[x][y] + 1);
                    }
                    if (dist[x][y] >= 1 && cnt >= 2)
                        ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4 + 4);
                    if (dist[x][y] == 0 && cnt >= 3)
                        ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4 + 4);
                }
            }
            for (int x = 1; x < H - 1; x++) {
                for (int y = 1; y < W - 1; y++) {
                    if (dist[x][y] <= 0)
                        continue;
                    int cnt = 0;
                    for (int i = 0; i < 4; i++) {
                        int nxt_x = x + dx[i], nxt_y = y + dy[i];
                        if (dist[nxt_x][nxt_y] == -1)
                            continue;
                        cnt += (dist[nxt_x][nxt_y] == dist[x][y] - 1);
                    }
                    if (cnt >= 2)
                        ans = min(ans, dist[bx][by] * 2 + dist[x][y] * 4);
                }
            }
        }

        if (ans == (int)1e9)
            ans = -1;
        cout << ans << endl;
    }
}
0