結果
問題 | No.157 2つの空洞 |
ユーザー |
|
提出日時 | 2024-04-03 09:41:14 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,422 bytes |
コンパイル時間 | 1,478 ms |
コンパイル使用メモリ | 158,452 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-09-30 23:40:55 |
合計ジャッジ時間 | 2,336 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 16 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long ll;int w, h;char a[100][100];int dx[]={1, 0, -1, 0};int dy[]={0, 1, 0, -1};int cnt = 1;void dfs(int x, int y, int cnt) {a[x][y] = '0' + cnt;for (int i = 0; i < 4; i++) {if (a[x+dx[i]][y+dy[i]] == '.') {dfs(x+dx[i], y+dy[i], cnt);}}}int main() {ios::sync_with_stdio(false); cin.tie(0);//freopen("input.txt", "r", stdin);cin >> w >> h;for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {cin >> a[i][j];}}for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {if (a[i][j] == '.') {dfs(i, j, cnt);cnt = cnt + 1;}}}int ans = INT_MAX;for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {if (a[i][j] == '1') {for(int k = 0; k < h; k++) {for (int m = 0; m < w; m++) {if (a[k][m] == '2') {ans = min(ans, abs(i-k) + abs(j-m));}}}}}}cout << ans -1 << endl;// for (int i = 0; i < h; i++) {// for (int j = 0; j < w; j++) {// cout << a[i][j];// }// cout << "\n";// }return 0;}