結果

問題 No.2731 Two Colors
ユーザー 👑 nu50218nu50218
提出日時 2024-04-19 22:24:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 364 ms / 3,000 ms
コード長 2,307 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 202,672 KB
実行使用メモリ 16,624 KB
最終ジャッジ日時 2024-04-19 22:24:49
合計ジャッジ時間 7,469 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 364 ms
12,416 KB
testcase_01 AC 331 ms
12,288 KB
testcase_02 AC 330 ms
12,288 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 14 ms
5,888 KB
testcase_05 AC 11 ms
6,116 KB
testcase_06 AC 31 ms
6,240 KB
testcase_07 AC 34 ms
7,040 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 115 ms
12,016 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 137 ms
16,624 KB
testcase_12 AC 120 ms
12,000 KB
testcase_13 AC 127 ms
14,552 KB
testcase_14 AC 55 ms
11,008 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 58 ms
8,464 KB
testcase_17 AC 72 ms
9,004 KB
testcase_18 AC 14 ms
5,376 KB
testcase_19 AC 61 ms
11,376 KB
testcase_20 AC 69 ms
10,204 KB
testcase_21 AC 22 ms
10,496 KB
testcase_22 AC 125 ms
11,608 KB
testcase_23 AC 37 ms
9,512 KB
testcase_24 AC 23 ms
7,736 KB
testcase_25 AC 7 ms
7,808 KB
testcase_26 AC 100 ms
9,872 KB
testcase_27 AC 120 ms
14,100 KB
testcase_28 AC 83 ms
9,964 KB
testcase_29 AC 31 ms
5,376 KB
testcase_30 AC 50 ms
9,548 KB
testcase_31 AC 36 ms
9,004 KB
testcase_32 AC 175 ms
12,184 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define postprocess(...) ((void)0)

using namespace std;
using ll = long long;
using ld = long double;

int A[1010][1010];
bool colored[1010][1010];
int color[1010][1010];

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

void solve([[maybe_unused]] int test) {
    int H, W;
    cin >> H >> W;

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
            A[i][j] *= -1;
        }
    }

    colored[0][0] = true;
    colored[H - 1][W - 1] = true;
    color[0][0] = 1;
    color[H - 1][W - 1] = 0;

    priority_queue<pair<int, pair<int, int>>> pque0, pque1;
    pque0.push({A[1][0], {1, 0}});
    pque0.push({A[0][1], {0, 1}});
    pque1.push({A[H - 2][W - 1], {H - 2, W - 1}});
    pque1.push({A[H - 1][W - 2], {H - 1, W - 2}});

    int round = 1;
    while (true) {
        assert(!pque0.empty());

        bool done = false;
        int nx = 0;
        int ny = 0;

        while (!pque0.empty()) {
            auto f = pque0.top();
            pque0.pop();

            auto [x, y] = f.second;
            if (colored[x][y]) {
                continue;
            }

            nx = x;
            ny = y;
            color[nx][ny] = 2 - (round % 2);
            colored[nx][ny] = true;
            done = true;

            for (int k = 0; k < 4; k++) {
                int nx2 = nx + dx[k];
                int ny2 = ny + dy[k];

                if (nx2 < 0 || nx2 >= H || ny2 < 0 || ny2 >= W) {
                    continue;
                }

                pque0.push({A[nx2][ny2], {nx2, ny2}});
            }

            break;
        }

        assert(done);
        bool finished = false;
        for (int k = 0; k < 4; k++) {
            int nx2 = nx + dx[k];
            int ny2 = ny + dy[k];

            if (nx2 < 0 || nx2 >= H || ny2 < 0 || ny2 >= W) {
                continue;
            }

            if ((color[nx][ny] | color[nx2][ny2]) == 3) finished = true;
        }
        if (finished) break;

        round++;
        swap(pque0, pque1);
    }

    cout << round << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve(i);
    }

    postprocess();
}
0