結果

問題 No.157 2つの空洞
コンテスト
ユーザー ゴリポン先生
提出日時 2025-11-26 21:54:44
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 2,169 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,314 ms
コンパイル使用メモリ 202,836 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-11-26 21:54:50
合計ジャッジ時間 4,374 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

module main;
// https://yukicoder.me/submissions/16998 より
// 2次元グリッド、幅優先探索
import std;

// C++ の tie
auto tie(StoreElements...)(ref StoreElements stores)
{
	alias Elements = staticMap!(Unqual, StoreElements);
	
	template toPointer(T)
	{
		alias toPointer = T*;
	}
	
	struct Holder
	{
		alias StoreElementsPtrs = staticMap!(toPointer, StoreElements);
		StoreElementsPtrs storePtrs;
		
		this(ref StoreElements stores)
		{
			foreach(i, _; StoreElements)
			{
				storePtrs[i] = &stores[i];
			}
		}
		
		void opAssign(Tuple!Elements values)
		{
			foreach(i, _; Elements)
			{
				*storePtrs[i] = values[i];
			}
		}
	}
	
	return Holder(stores);
}

void main()
{
	// 入力
	int W, H;
	readln.chomp.formattedRead("%d %d", W, H);
	auto board = new string[](H);
	foreach (ref row; board)
		row = readln.chomp;
	// 答えの計算と出力
	auto check = new bool[][](H, W);
	alias P = Tuple!(int, "y", int, "x");
	auto que = DList!P();
	foreach (i; 0 .. H) {
		foreach (j; 0 .. W) {
			if (board[i][j] == '#')
				continue;
			check[i][j] = true;
			que.insertBack(P(i, j));
			break;
		}
		if (!que.empty)
			break;
	}

	auto dir = [P(1, 0), P(0, 1), P(-1, 0), P(0, -1)];
	while (!que.empty) {
		int y, x;
		tie(y, x) = que.front;
		que.removeFront;
		foreach (d; dir) {
			int ny = y + d.y;
			int nx = x + d.x;
			if (ny < 0 || ny >= H || nx < 0 || nx >= W)
				continue;
			if (check[ny][nx])
				continue;
			if (board[ny][nx] == '#')
				continue;
			check[ny][nx] = true;
			que.insertBack(P(ny, nx));
		}
	}

	auto dist = new int[][](H, W);
	foreach (i; 0 .. H) {
		foreach (j; 0 .. W) {
			if (board[i][j] == '.' && !check[i][j]) {
				check[i][j] = true;
				que.insertBack(P(i, j));
				dist[i][j] = 0;
			} else
				dist[i][j] = 999;
		}
	}

	while (!que.empty) {
		int y, x;
		tie(y, x) = que.front;
		que.removeFront;
		foreach (d; dir) {
			int ny = y + d.y;
			int nx = x + d.x;
			if (ny < 0 || ny >= H || nx < 0 || nx >= W)
				continue;
			if (dist[ny][nx] < 999)
				continue;
			if (board[ny][nx] == '.') {
				writeln(dist[y][x]);
				return;
			}
			que.insertBack(P(ny, nx));
			dist[ny][nx] = dist[y][x] + 1;
		}
	}
}
0