結果

問題 No.157 2つの空洞
ユーザー sggkshio
提出日時 2016-12-07 01:21:26
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,193 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 92,548 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-28 03:19:47
合計ジャッジ時間 1,731 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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