結果

問題 No.157 2つの空洞
ユーザー kyuridenamidakyuridenamida
提出日時 2016-08-25 16:01:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,970 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 101,188 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 15:18:35
合計ジャッジ時間 1,537 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_set>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <sstream>
#include <complex>
#include <bitset>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rev(i,n) for(int i=(n)-1;(i)>=0;(i)--)
#define all(a) (a).begin(),(a).end()
#define pb(a) push_back(a)
#define bitcount(b) __builtin_popcount(b)
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 

int done[20][20];
char g[20][20];
struct NODE{
	int x,y,c;
};
queue<NODE> Q;
void dfs(int x,int y,int c){
	if( g[y][x] != c ) return;
	if( done[y][x] ) return;
	done[y][x] = true;
	Q.push({x,y,0});
	dfs(x,y-1,c);
	dfs(x+1,y,c);
	dfs(x,y+1,c);
	dfs(x-1,y,c);
}

int main(){
	int H,W;
	cin >> W >> H;
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			cin >> g[i][j];
		}
	}
	int ok = 0;
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			if( g[i][j] == '.' ){
				dfs(j,i,g[i][j]);
				ok = 1;
			}
			if(ok) break;
		}
		if(ok) break;
	}
	int okok[25][25] = {};
	while( Q.size() ){
		auto q = Q.front(); Q.pop();
		if( q.y < 0 || q.x < 0 || q.x >= W || q.y >= H ) continue;

		if( okok[q.y][q.x]++) continue;
		if( g[q.y][q.x] == '.' and done[q.y][q.x] == 0 ){
			cout << q.c - 1 << endl;
			return 0;
		}
		Q.push({q.x,q.y+1,q.c+1});
		Q.push({q.x+1,q.y,q.c+1});
		Q.push({q.x,q.y-1,q.c+1});
		Q.push({q.x-1,q.y,q.c+1});

	}
}
0