結果

問題 No.157 2つの空洞
ユーザー ty70ty70
提出日時 2015-05-27 11:11:55
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,801 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 93,620 KB
実行使用メモリ 814,416 KB
最終ジャッジ日時 2023-09-20 15:31:24
合計ジャッジ時間 5,855 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<10

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PI;

const int dr[4] = {-1, 0, 1, 0 };
const int dc[4] = { 0, 1, 0,-1 };

int W, H;
char cave[22][22];
int used[22][22];

void dfs (int r, int c, char ch1, char ch2 ){ // 現在位置 (r, c ) 通過可能文字 ch1 上書き文字 ch2 
	if (r < 0 || r >= H || c < 0 || c >= W ) return;
	if (cave[r][c] != ch1 ) return;	
	
	cave[r][c] = ch2;
	rep (k, 4 )
		dfs (r+dr[k], c+dc[k], ch1, ch2 );
}

int bfs (int r, int c, char ch1, char ch2 ){	// 現在位置 (r, c ) 通過可能文字 ch1 探索文字 ch2 
	memset (used, -1, sizeof (used ) );
	used[r][c] = 0;
	queue<PI> que;
	que.push (PI(P (r, c ), 0 ) );

	while (!que.empty() ){
		PI curr = que.front(); que.pop();
		int cr = curr.first.first;
		int cc = curr.first.second;
		int cn = curr.second;
		used[cr][cc] = cn;
		rep (k, 4 ){
			int nr = cr + dr[k];
			int nc = cc + dc[k];
			if (nr < 0 || nr >= H || nc < 0 || nc >= W ) continue;
			if (cave[nr][nc] == ch2 ) return cn;
			if (cave[nr][nc] != ch1 && cave[nr][nc] != '1' ) continue;
			if (used[nr][nc] == -1 || used[nr][nc] > used[r][c] + 1 ){
				que.push (PI( P (nr, nc ), cn + 1 ) );
			} // end if
		} // end rep
	} // end while

	return -1;
} 	

void disp_cave (void ){
	rep (i, H ){
		rep (j, W ){
			cerr << cave[i][j];
		} // end rep
		cerr << endl;
	} // end rep
}

void disp_used (void ){
	cerr << endl;
	rep (i, H ){
		rep (j, W ){
			cerr << setw(2) << used[i][j];
		} // end rep
		cerr << endl;
	} // end rep
}

int main()
{
	memset (cave, 0, sizeof (cave ) );
	ios_base::sync_with_stdio(0);
	cin >> W >> H;
	rep (i, H ) rep (j, W ) cin >> cave[i][j];
	int cnt = 0;
	rep (i, H ) rep (j, W )
		if (cave[i][j] == '.' ){
			dfs (i, j, '.', (char)('1'+cnt) );
			cnt++;
		} // end if			
//	disp_cave ();

	int res = INF;
	rep (i, H ) rep (j, W ) if (cave[i][j] == '1' ){
		int curr = bfs (i, j, '#', '2' );
		res = min (res, curr );
//		disp_used ();
	} // end if
	cout << res << endl;

	return 0;
}
0