結果

問題 No.157 2つの空洞
ユーザー kpinkcatkpinkcat
提出日時 2023-11-02 18:02:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,010 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 208,496 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-11-02 18:02:50
合計ジャッジ時間 7,662 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,027 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<bitset>
using ll = long long;
using namespace std;

vector<int> dx{1, 0, -1, 0}, dy{0, 1, 0, -1};
void bfs1(vector<string> &v, const int x, const int y){
    int nx, ny;
    for (int i = 0; i < 4; i++){
        nx = x + dx[i];
        ny = y + dy[i];
        if (nx >= 0 && nx < v[0].size() && ny >= 0 && ny < v.size() && (v[ny][nx] == '.')) {
            v[ny][nx] = '*';
            bfs1(v, nx, ny);
        }
    }
}

int bfs2(vector<string> &v, const int x, const int y, int ans){
    int nx, ny, min1 = min(v.size()-2, v[0].size()-2);
    vector<string> tv;
    tv = v;
    if (ans >= min1) return min1;
    for (int i = 0; i < 4; i++){
        int tmp = ans;
        nx = x + dx[i];
        ny = y + dy[i];
        if (nx > 0 && nx < tv[0].size() - 1 && ny > 0 && ny < tv.size() -1){
            if (tv[ny][nx] == '#'){
                tmp++;
                tv[ny][nx] = ' ';
                tmp = bfs2(tv, nx, ny, tmp);
            } else if (tv[ny][nx] == '*'){
                tmp = tmp;
            } else tmp = min(v.size()-2, v[0].size() -2);
        } else tmp = min(v.size()-2, v[0].size() -2);
        min1 = min(min1, tmp);
    }
    return min1;
}

int main(){
    int w, h;
    cin >> w >> h;
    vector<string> v(h);
    
    for (int i = 0; i < h; i++) cin >> v[i];
    bool ast = false;
    for (int i = 1; i < h - 1; i++) {
        for (int j = 1; j < w - 1; j++){
            if (v[i][j] == '.'){
                bfs1(v, j, i);
                ast = true;
                break;
            }
        }
        if (ast) break;
    }
    ast = false;
    int ans = v[0].size() -2;
    for (int i = 1; i < h - 1; i++) {
        for (int j = 1; j < w - 1; j++){
            if (v[i][j] == '.'){
                ans = min(ans, bfs2(v, j, i, 0));
            }
        }
    }
    cout << ans << endl;
}
0