結果

問題 No.2731 Two Colors
ユーザー kusirakusirakusirakusira
提出日時 2024-03-14 20:44:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 482 ms / 3,000 ms
コード長 1,652 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 91,232 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-14 14:26:13
合計ジャッジ時間 8,572 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 481 ms
19,200 KB
testcase_01 AC 480 ms
19,456 KB
testcase_02 AC 482 ms
19,200 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 23 ms
6,944 KB
testcase_05 AC 15 ms
6,944 KB
testcase_06 AC 63 ms
6,944 KB
testcase_07 AC 77 ms
6,940 KB
testcase_08 AC 12 ms
6,940 KB
testcase_09 AC 283 ms
15,044 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 256 ms
17,336 KB
testcase_12 AC 281 ms
14,824 KB
testcase_13 AC 215 ms
12,832 KB
testcase_14 AC 122 ms
8,576 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 104 ms
8,140 KB
testcase_17 AC 155 ms
10,000 KB
testcase_18 AC 26 ms
6,944 KB
testcase_19 AC 108 ms
8,312 KB
testcase_20 AC 186 ms
10,852 KB
testcase_21 AC 28 ms
6,944 KB
testcase_22 AC 281 ms
15,516 KB
testcase_23 AC 68 ms
6,940 KB
testcase_24 AC 34 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 231 ms
12,836 KB
testcase_27 AC 290 ms
15,884 KB
testcase_28 AC 172 ms
11,752 KB
testcase_29 AC 53 ms
6,940 KB
testcase_30 AC 102 ms
8,148 KB
testcase_31 AC 61 ms
6,944 KB
testcase_32 AC 335 ms
17,780 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,944 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