結果

問題 No.2731 Two Colors
ユーザー AngrySadEightAngrySadEight
提出日時 2024-03-14 17:56:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 3,000 ms
コード長 1,652 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 91,288 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-04-14 14:25:36
合計ジャッジ時間 8,347 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 480 ms
19,200 KB
testcase_01 AC 478 ms
19,200 KB
testcase_02 AC 477 ms
19,200 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 24 ms
6,940 KB
testcase_05 AC 15 ms
6,940 KB
testcase_06 AC 62 ms
6,940 KB
testcase_07 AC 76 ms
6,940 KB
testcase_08 AC 11 ms
6,944 KB
testcase_09 AC 281 ms
14,920 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 254 ms
17,080 KB
testcase_12 AC 281 ms
14,900 KB
testcase_13 AC 213 ms
12,952 KB
testcase_14 AC 121 ms
8,448 KB
testcase_15 AC 10 ms
6,944 KB
testcase_16 AC 104 ms
8,136 KB
testcase_17 AC 154 ms
9,880 KB
testcase_18 AC 26 ms
6,944 KB
testcase_19 AC 107 ms
8,192 KB
testcase_20 AC 187 ms
10,852 KB
testcase_21 AC 28 ms
6,944 KB
testcase_22 AC 281 ms
15,512 KB
testcase_23 AC 67 ms
6,940 KB
testcase_24 AC 33 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 228 ms
12,840 KB
testcase_27 AC 283 ms
16,016 KB
testcase_28 AC 171 ms
11,696 KB
testcase_29 AC 53 ms
6,940 KB
testcase_30 AC 101 ms
8,148 KB
testcase_31 AC 61 ms
6,940 KB
testcase_32 AC 335 ms
17,776 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

int main() {
    ll H, W;
    cin >> H >> W;
    vector<vector<ll>> A(H, vector<ll>(W));
    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {
            cin >> A[i][j];
        }
    }
    vector<vector<ll>> color(H, vector<ll>(W, -1));
    vector<priority_queue<pll, vector<pll>, greater<pll>>> pque(2);
    pque[0].push(pll(A[0][0], 0));
    pque[1].push(pll(A[H - 1][W - 1], (H - 1) * W + (W - 1)));
    vector<ll> dx = {1, 0, -1, 0};
    vector<ll> dy = {0, 1, 0, -1};
    ll cnt = 0;
    while (true) {
        bool finish = false;
        ll cnt_mod = cnt % 2;
        pll p = pque[cnt_mod].top();
        pque[cnt_mod].pop();
        ll px = p.second / W;
        ll py = p.second % W;
        if (color[px][py] != -1) {
            continue;
        }
        color[px][py] = cnt_mod;
        for (ll i = 0; i < 4; i++) {
            if (px + dx[i] >= 0 && px + dx[i] < H && py + dy[i] >= 0 &&
                py + dy[i] < W) {
                ll new_c = color[px + dx[i]][py + dy[i]];
                if (new_c != -1) {
                    if (new_c != color[px][py]) {
                        finish = true;
                        break;
                    }
                } else {
                    pque[cnt_mod].push(pll(A[px + dx[i]][py + dy[i]],
                                           (px + dx[i]) * W + (py + dy[i])));
                }
            }
        }
        cnt++;
        if (finish) {
            break;
        }
    }
    cout << cnt - 2 << endl;
}
0