結果

問題 No.157 2つの空洞
ユーザー kpinkcatkpinkcat
提出日時 2023-11-02 18:45:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,399 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 210,296 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-02 18:45:24
合計ジャッジ時間 3,594 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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};
vector<vector<pair<int, int>>> pos(2);

void bfs1(vector<string> &v, const int x, const int y, const int cnt){
    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] = '*';
            pos[cnt].push_back(make_pair(ny, nx));
            bfs1(v, nx, ny, cnt);
        }
    }
}

int main(){
    int w, h, cnt = 0, min1 = 50;
    cin >> w >> h;
    vector<string> v(h);
    for (int i = 0; i < h; i++) cin >> v[i];
    int ast = 0;
    for (int i = 1; i < h - 1; i++) {
        for (int j = 1; j < w - 1; j++){
            if (v[i][j] == '.'){
                v[i][j] = '*';
                pos[cnt].push_back(make_pair(i, j));
                bfs1(v, j, i, cnt);
                cnt++;
                ast++;
            }
        }
        if (ast == 2) break;
    }
    
    for (auto x : pos[0]){
        for (auto y : pos[1]){
            min1 = min(min1, abs(y.first - x.first) + abs(y.second - x.second) -1);
        }
    }
    cout << min1 << endl;
}
0