結果

問題 No.2731 Two Colors
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-04-25 15:53:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 3,000 ms
コード長 2,751 bytes
コンパイル時間 2,708 ms
コンパイル使用メモリ 208,888 KB
実行使用メモリ 12,488 KB
最終ジャッジ日時 2024-04-25 15:53:45
合計ジャッジ時間 9,030 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 284 ms
11,136 KB
testcase_01 AC 261 ms
11,264 KB
testcase_02 AC 261 ms
11,008 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 11 ms
6,940 KB
testcase_05 AC 8 ms
6,940 KB
testcase_06 AC 28 ms
6,940 KB
testcase_07 AC 31 ms
6,940 KB
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 105 ms
9,592 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 122 ms
12,488 KB
testcase_12 AC 106 ms
9,452 KB
testcase_13 AC 100 ms
9,268 KB
testcase_14 AC 48 ms
6,940 KB
testcase_15 AC 7 ms
6,940 KB
testcase_16 AC 49 ms
6,940 KB
testcase_17 AC 66 ms
7,140 KB
testcase_18 AC 13 ms
6,944 KB
testcase_19 AC 51 ms
6,944 KB
testcase_20 AC 68 ms
7,360 KB
testcase_21 AC 14 ms
6,940 KB
testcase_22 AC 115 ms
10,144 KB
testcase_23 AC 30 ms
6,944 KB
testcase_24 AC 16 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 89 ms
8,572 KB
testcase_27 AC 112 ms
10,488 KB
testcase_28 AC 77 ms
8,188 KB
testcase_29 AC 25 ms
6,940 KB
testcase_30 AC 43 ms
6,940 KB
testcase_31 AC 30 ms
6,944 KB
testcase_32 AC 145 ms
11,496 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;

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

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

    int H, W, ans=0, x, y, cnt=0, a, xx, yy;
    cin >> H >> W;
    vector used(H, vector<int>(W, -1));
    pq<tuple<int, int, int>> que0, que1;
    vector A(H, vector<int>(W));
    for (int i=0; i<H; i++){
        for (int j=0; j<W; j++) cin >> A[i][j];
    }
    used[0][0] = 1;
    used[H-1][W-1] = 0;

    auto check=[&](int i, int j)->bool{
        return 0 <= i & i < H & 0 <= j & j < W;
    };

    for (int i=0; i<4; i++){
        x = dx[i];
        y = dy[i];
        if (check(x, y)){
            que1.push({A[x][y], x, y});
        }
    }
    for (int i=0; i<4; i++){
        x = dx[i]+H-1;
        y = dy[i]+W-1;
        if (check(x, y)){
            que0.push({A[x][y], x, y});
        }
    }

    while(1){
        cnt++;
        cnt %= 2;
        if (cnt == 1){
            while(!que1.empty()){
                tie(a, x, y) = que1.top();
                que1.pop();
                if (used[x][y] == -1){
                    used[x][y] = 1;
                    ans++;
                    for (int j=0; j<4; j++){
                        xx = x+dx[j];
                        yy = y+dy[j];
                        if (check(xx, yy)){
                            if (used[xx][yy] == 0){
                                cout << ans << endl;
                                return 0;
                            }
                            else if (used[xx][yy] == -1){
                                que1.push({A[xx][yy], xx, yy});
                            }
                        }
                    }
                    break;
                }
            }
        }
        else{
            while(!que0.empty()){
                tie(a, x, y) = que0.top();
                que0.pop();
                if (used[x][y] == -1){
                    used[x][y] = 0;
                    ans++;
                    for (int j=0; j<4; j++){
                        xx = x+dx[j];
                        yy = y+dy[j];
                        if (check(xx, yy)){
                            if (used[xx][yy] == 1){
                                cout << ans << endl;
                                return 0;
                            }
                            else if (used[xx][yy] == -1){
                                que0.push({A[xx][yy], xx, yy});
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0