結果

問題 No.2731 Two Colors
ユーザー InTheBloomInTheBloom
提出日時 2024-04-19 22:04:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 3,000 ms
コード長 2,581 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 106,664 KB
実行使用メモリ 9,948 KB
最終ジャッジ日時 2024-04-19 22:04:46
合計ジャッジ時間 8,425 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 468 ms
7,808 KB
testcase_01 AC 454 ms
7,808 KB
testcase_02 AC 461 ms
7,936 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 22 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 60 ms
5,376 KB
testcase_07 AC 73 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 279 ms
7,512 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 242 ms
9,948 KB
testcase_12 AC 273 ms
7,248 KB
testcase_13 AC 203 ms
7,764 KB
testcase_14 AC 120 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 98 ms
5,528 KB
testcase_17 AC 152 ms
6,192 KB
testcase_18 AC 26 ms
5,376 KB
testcase_19 AC 102 ms
5,724 KB
testcase_20 AC 185 ms
6,128 KB
testcase_21 AC 28 ms
5,376 KB
testcase_22 AC 271 ms
8,200 KB
testcase_23 AC 65 ms
5,376 KB
testcase_24 AC 33 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 222 ms
6,804 KB
testcase_27 AC 274 ms
8,332 KB
testcase_28 AC 167 ms
7,128 KB
testcase_29 AC 51 ms
5,376 KB
testcase_30 AC 100 ms
5,676 KB
testcase_31 AC 58 ms
5,376 KB
testcase_32 AC 330 ms
9,104 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

constexpr int iINF = 1'000'000'000;

int main () {
    int H, W; cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) cin >> A[i][j];
    }

    // グリッド上のダイクストラ法に帰着

    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq1, pq2;
    vector<vector<bool>> vis1(H, vector<bool>(W, false)), vis2(H, vector<bool>(W, false));

    pq1.push(make_pair(A[0][0], make_pair(0, 0)));
    pq2.push(make_pair(A[H - 1][W - 1], make_pair(H - 1, W - 1)));

    const vector<pair<int, int>> dxy = {
        make_pair(0, 1),
        make_pair(0, -1),
        make_pair(1, 0),
        make_pair(-1, 0),
    };

    bool end = false;
    int ans = 0;

    while (true) {
        {
            int y, x, c;
            while (true) {
                auto h = pq1.top(); pq1.pop();
                c = h.first;
                y = h.second.first, x = h.second.second;

                if (vis1[y][x]) continue;
                break;
            }
            vis1[y][x] = true;
            ans++;

            for (auto d : dxy) {
                int ny = y + d.first, nx = x + d.second;
                if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
                if (vis1[ny][nx]) continue;

                // 相手側がもう塗られている
                if (vis2[ny][nx]) {
                    end = true;
                    break;
                }

                pq1.push(make_pair(A[ny][nx], make_pair(ny, nx)));
            }
        }

        if (end) break;

        {
            int y, x, c;
            while (true) {
                auto h = pq2.top(); pq2.pop();
                c = h.first;
                y = h.second.first, x = h.second.second;

                if (vis2[y][x]) continue;
                break;
            }
            vis2[y][x] = true;
            ans++;

            for (auto d : dxy) {
                int ny = y + d.first, nx = x + d.second;
                if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
                if (vis2[ny][nx]) continue;

                // 相手側がもう塗られている
                if (vis1[ny][nx]) {
                    end = true;
                    break;
                }

                pq2.push(make_pair(A[ny][nx], make_pair(ny, nx)));
            }
        }

        if (end) break;
    }

    cout << ans - 2 << "\n";
}
0