結果

問題 No.157 2つの空洞
ユーザー nkshnnkshn
提出日時 2016-08-25 16:49:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,187 bytes
コンパイル時間 555 ms
コンパイル使用メモリ 73,256 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-07 20:58:14
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
	int W,H;
	cin>>W>>H;
	int C[22][22] = {};
	for(int i=1;i<=H;i++){
		string str;
		cin>>str;
		for(int j=1;j<=W;j++){
			if(str[j]=='.'){
				C[i][j] = 1;
			}
		}
	}
	queue<pair<int,int> > que;
	int dx[4]={1,0,-1,0};
	int dy[4]={0,1,0,-1}; 
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			if(C[i][j] == 1){
				que.push(make_pair(i,j));
				C[i][j] = 2;
				while(!que.empty()){
					int y = que.front().first;
					int x = que.front().second;
					que.pop();
					for(int k=0;k<4;k++){
						if(C[dy[k]+y][dx[k]+x]==1){
							C[dy[k]+y][dx[k]+x] = 2;
							que.push(make_pair(dy[k]+y,dx[k]+x));
						}
					}
				}				int res = 1000000;
				for(int i1=1;i1<=H;i1++){
					for(int i2=1;i2<=W;i2++){
						if(C[i1][i2]==1){
							for(int j1=1;j1<=H;j1++){
								for(int j2=1;j2<=W;j2++){
									if(C[j1][j2]==2){
										//cout<<i1<<","<<i2<<","<<j1<<","<<j2<<endl;
										res = min(res,abs(j1-i1) + abs(j2-i2)-1);
										//cout<<res<<endl;
									}
								}
							}
						}
					}
				}
				cout<<res<<endl;
				return 0;
			}
		}
	}

}
0