結果

問題 No.157 2つの空洞
ユーザー GOTKAKO
提出日時 2025-09-02 11:48:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,592 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 209,504 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-02 11:48:52
合計ジャッジ時間 3,563 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> BFS01_2D(const vector<string> &S,int sx,int sy,char C){ //C=障害物.
    int H = S.size(),W = S.at(0).size(); 
    vector<vector<int>> ret(H,vector<int>(W,1001001001));
    deque<pair<int,int>> Q; Q.push_back({sx,sy});
    ret.at(sx).at(sy) = 0;
    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    while(Q.size()){
        auto[x,y] = Q.front(); Q.pop_front();
        for(auto &[dx,dy] : dxy){
            int nx = dx+x,ny = dy+y;
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(S.at(nx).at(ny) == C) continue; //通れない条件.
            int cost = 0;
            if(S.at(nx).at(ny) == '#') cost = 1;
            if(ret.at(nx).at(ny) <= ret.at(x).at(y)+cost) continue;
            ret.at(nx).at(ny) = ret.at(x).at(y)+cost;
            if(cost == 0) Q.push_front({nx,ny}); 
            else Q.push_back({nx,ny});
        }
    }
    for(auto &D : ret) for(auto &d : D) if(d == 1001001001) d = -1; 
    return ret;
    //場合によっては書き換える!.
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int H,W; cin >> H >> W;
    swap(H,W);
    vector<string> S(H);
    for(auto &s : S) cin >> s;
    for(int i=0; i<H; i++) for(int k=0; k<W; k++) if(S.at(i).at(k) == '.'){
        auto dist = BFS01_2D(S,i,k,'-');
        int answer = 0;
        for(int x=0; x<H; x++) for(int y=0; y<W; y++) if(S.at(x).at(y) == '.'){
            answer = max(answer,dist.at(x).at(y));
        }
        cout << answer << endl; return 0;
    }
}
0