結果

問題 No.2731 Two Colors
ユーザー GOTKAKOGOTKAKO
提出日時 2024-04-19 21:39:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 277 ms / 3,000 ms
コード長 1,563 bytes
コンパイル時間 2,945 ms
コンパイル使用メモリ 217,960 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-04-19 21:39:44
合計ジャッジ時間 7,373 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
7,680 KB
testcase_01 AC 268 ms
7,808 KB
testcase_02 AC 273 ms
7,808 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 26 ms
5,376 KB
testcase_07 AC 27 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 101 ms
7,408 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 123 ms
9,856 KB
testcase_12 AC 107 ms
7,276 KB
testcase_13 AC 98 ms
7,792 KB
testcase_14 AC 45 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 48 ms
5,376 KB
testcase_17 AC 67 ms
6,056 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 51 ms
5,488 KB
testcase_20 AC 64 ms
5,888 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 111 ms
8,228 KB
testcase_23 AC 29 ms
5,376 KB
testcase_24 AC 17 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 87 ms
6,948 KB
testcase_27 AC 113 ms
8,356 KB
testcase_28 AC 77 ms
7,024 KB
testcase_29 AC 23 ms
5,376 KB
testcase_30 AC 42 ms
5,572 KB
testcase_31 AC 31 ms
5,376 KB
testcase_32 AC 138 ms
8,996 KB
testcase_33 AC 2 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>
using namespace std;

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

    int H,W; cin >> H >> W;
    vector<vector<int>> A(H,vector<int>(W));
    for(int i=0; i<H; i++) for(int k=0; k<W; k++) cin >> A.at(i).at(k);
    
    vector<vector<bool>> alr(H,vector<bool>(W)),alb = alr;
    priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<>> qr,qb;
    
    bool end = false;
    int answer = -2;
    qr.push({0,0,0}),qb.push({0,H-1,W-1});
    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    while(true){
        auto[a,x,y] = qr.top(); qr.pop();
        while(alr.at(x).at(y)){tie(a,x,y) = qr.top(); qr.pop();}
        alr.at(x).at(y) = true;
        answer++;
        for(auto &[dx,dy] : dxy){
            int nx = x+dx,ny = y+dy;
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(alr.at(nx).at(ny)) continue;
            if(alb.at(nx).at(ny)){end = true; break;}
            qr.push({A.at(nx).at(ny),nx,ny});
        }
        if(end) break;
    
        tie(a,x,y) = qb.top(); qb.pop();
        while(alb.at(x).at(y)){tie(a,x,y) = qb.top(); qb.pop();}
        alb.at(x).at(y) = true;
        answer++;
        for(auto &[dx,dy] : dxy){
            int nx = x+dx,ny = y+dy;
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(alb.at(nx).at(ny)) continue;
            if(alr.at(nx).at(ny)){end = true; break;}
            qb.push({A.at(nx).at(ny),nx,ny});  
        }
        if(end) break;
    }
    cout << answer << endl;
}
0