結果

問題 No.157 2つの空洞
ユーザー naimonon77naimonon77
提出日時 2016-09-22 17:43:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,752 bytes
コンパイル時間 1,557 ms
コンパイル使用メモリ 171,928 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 01:17:37
合計ジャッジ時間 2,788 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) begin(a),end(a)
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else 
const bool test = false;
#endif

int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)1e6 + 5

char field[30][30];
int fieldv[30][30];

struct Point {
	int y, x, cnt;
};

queue<Point> q;
int H, W;

bool ng(int y, int x) {
	return y < 0 || H <= y || x < 0 || W <= x;
}

void dfs(int y,int x) {
	int i, j;
	field[y][x] = '!';
	q.push({ y,x,0 });
	fieldv[y][x] = 0;

	rep(i, 4) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (ng(ny, nx)) continue;
		if (field[ny][nx] == '.') {
			dfs(ny, nx);
		}
	}
}

void grid_bfs() {
	int i, j;
	//.............
	cin >> W >> H;
	rep(i, H) {
		cin >> field[i];
	}
	//..............
	rep(i, H) rep(j, W) fieldv[i][j] = INF;
	rep(i, H) rep(j, W) {
		if (field[i][j] == '.') {
			dfs(i, j);
			goto part1;
		}
	}
part1:
	rep(i, H) {
		cerr << field[i] << endl;
	}
	while (q.size()) {
		auto now = q.front(); q.pop();
		auto next = now;
		next.cnt++;
		rep(i, 4) {
			next.y = now.y + dy[i];
			next.x = now.x + dx[i];
			if (ng(next.y, next.x)) continue;

			if (fieldv[next.y][next.x] == INF) {
				if (field[next.y][next.x] == '.') {
					cout << now.cnt << endl;
					return;
				}
				fieldv[next.y][next.x] = next.cnt;
				q.push(next);
			}
		}
	}
}

signed main(void) {
	int i, j, k, l;
	grid_bfs();
	return 0;
}
0