結果

問題 No.157 2つの空洞
ユーザー sggkshiosggkshio
提出日時 2016-12-07 01:21:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,193 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 88,392 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 19:43:04
合計ジャッジ時間 2,600 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

//#include<bits/stdc++.h>



using namespace std;




int main(){
	
	int a,b,ans=9999;
	cin>>a>>b;
	vector<string>p(b);
	for(int i=0;i<b;i++){
		cin>>p[i];
	}
	bool f=0;
	stack<int>x,y;
	stack<int>X,Y;
	vector<int>x1,y1,x2,y2;
	bool F[55][55]={};
	for(int i=0;i<b;i++){
		for(int j=0;j<a;j++){
			if(p[i][j]=='.'&&F[i][j]==0){
				x.push(j),y.push(i);
				int R=0;
				while(x.size()){
					//cout<<x.top()<<y.top()<<endl;;;
				//	int n=x.size();
					int r=x.top(),w=y.top();
					x1.push_back(r),y1.push_back(w);
					F[w][r]=1;
					if(p[w+1][r]=='.'&&!F[w+1][r])y.push(w+1),x.push(r);
					else if(p[w-1][r]=='.'&&!F[w-1][r])y.push(w-1),x.push(r);
					else if(p[w][r+1]=='.'&&!F[w][r+1])y.push(w),x.push(r+1);
					else if(p[w][r-1]=='.'&&!F[w][r-1])y.push(w),x.push(r-1);
				//	else if(n==x.size())break;
					else x.pop(),y.pop();
					
					
				}
				f=1;
			}
				if(f)break;
		}
		if(f)break;
	}
	f=0;
	
	for(int i=0;i<b;i++){
		for(int j=0;j<a;j++){
			if(p[i][j]=='.'&&F[i][j]==0){
				X.push(j),Y.push(i);
			
				int R=0;
				while(X.size()){
				//	int n=x.size();
					int r=X.top(),w=Y.top();
					x2.push_back(r),y2.push_back(w);
					F[w][r]=1;
					if(p[w+1][r]=='.'&&!F[w+1][r])Y.push(w+1),X.push(r);
					else if(p[w-1][r]=='.'&&!F[w-1][r])Y.push(w-1),X.push(r);
					else if(p[w][r+1]=='.'&&!F[w][r+1])Y.push(w),X.push(r+1);
					else if(p[w][r-1]=='.'&&!F[w][r-1])Y.push(w),X.push(r-1);
				//	else if(n==x.size())break;
					else X.pop(),Y.pop();
					
					
				}
				f=1; 
			}
			if(f)break;
		}
		if(f)break;
	}
	for(int i=0;i<x1.size();i++){
		int aa=x1[i],bb=y1[i];
		for(int j=0;j<x2.size();j++){
			
			int cc=x2[j],dd=y2[j];
			//cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<endl;
			int P=abs(aa-cc),Q=abs(bb-dd);
			if(P)P--;
			else if(Q)Q--;
			ans=min(ans,P+Q);
			//X.pop(),Y.pop();
		//////	X.push(cc),Y.push(dd);
		}
	//	x.pop(),y.pop();
		//	x.push(aa),y.push(bb);
	}
	cout<<ans<<endl;


	return 0;
	

}


	
0