結果
問題 | No.157 2つの空洞 |
ユーザー |
![]() |
提出日時 | 2023-11-02 18:45:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,399 bytes |
コンパイル時間 | 2,168 ms |
コンパイル使用メモリ | 200,448 KB |
最終ジャッジ日時 | 2025-02-17 17:19:14 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 16 |
ソースコード
#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;}