結果

問題 No.157 2つの空洞
ユーザー ty70ty70
提出日時 2015-05-27 12:09:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 93,592 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 15:39:03
合計ジャッジ時間 1,662 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 d[22][22];

void dfs (int r, int c, char ch1, char ch2 ){ // 現在位置 (r, c ) 通過可能文字 ch1 上書き文字 ch2 

	cave[r][c] = ch2;
	rep (k, 4 ){
		int nr = r + dr[k];
		int nc = c + dc[k];
		if (nr < 0 || nr >= H || nc < 0 || nc >= W ) continue;
		if (cave[nr][nc] != ch1 ) continue;
		dfs (nr, nc, ch1, ch2 );
	} // end rep
}

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

void disp_d (void ){
	cerr << endl;
	rep (i, H ){
		rep (j, W ){
			cerr << setw(2) << (d[i][j] == INF ? -1 : d[i][j]);
		} // end rep
		cerr << endl;
	} // end rep
}

int bfs (int r, int c, char ch1, char ch2 ){	// 現在位置 (r, c ) 通過可能文字 ch1 探索文字 ch2 

	rep (i, H ) rep (j, W ) d[i][j] = INF;
	d[r][c] = 0;
	queue<P> que;
	que.push (P (r, c ) );

	while (!que.empty() ){
		P curr = que.front(); que.pop();
		int cr = curr.first;
		int cc = curr.second;
//		disp_d ();
		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 d[cr][cc];
			if (cave[nr][nc] != ch1 && cave[nr][nc] != '1' ) continue;
			if (d[nr][nc] == INF ){
				que.push (P (nr, nc ) );
				d[nr][nc] = d[cr][cc] + (cave[nr][nc] == ch1 );
			} // end if
		} // end rep
	} // end while

	return INF;
} 	

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_d ();
	} // end if
	cout << res << endl;

	return 0;
}
0