結果

問題 No.2731 Two Colors
ユーザー GOTKAKOGOTKAKO
提出日時 2024-04-19 21:39:18
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 269 ms / 3,000 ms
コード長 1,563 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 223,592 KB
実行使用メモリ 10,552 KB
最終ジャッジ日時 2024-10-11 14:24:58
合計ジャッジ時間 6,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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