結果

問題 No.157 2つの空洞
ユーザー pekempeypekempey
提出日時 2015-10-11 19:16:50
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 1,290 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 77,848 KB
実行使用メモリ 80,728 KB
最終ジャッジ日時 2023-09-28 11:49:48
合計ジャッジ時間 3,545 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
77,952 KB
testcase_01 AC 93 ms
78,012 KB
testcase_02 AC 92 ms
77,948 KB
testcase_03 AC 95 ms
78,024 KB
testcase_04 AC 94 ms
77,980 KB
testcase_05 AC 92 ms
77,748 KB
testcase_06 AC 91 ms
77,736 KB
testcase_07 AC 90 ms
77,736 KB
testcase_08 AC 90 ms
77,624 KB
testcase_09 AC 91 ms
78,028 KB
testcase_10 AC 92 ms
78,008 KB
testcase_11 AC 94 ms
77,984 KB
testcase_12 AC 97 ms
77,948 KB
testcase_13 AC 155 ms
80,084 KB
testcase_14 AC 118 ms
80,632 KB
testcase_15 AC 113 ms
80,096 KB
testcase_16 AC 115 ms
79,640 KB
testcase_17 AC 116 ms
79,968 KB
testcase_18 AC 120 ms
80,380 KB
testcase_19 AC 118 ms
80,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import Queue

W, H = map(int, raw_input().split())
C = [raw_input() for _ in xrange(H)]

color = [[0 for _ in xrange(W)] for _ in xrange(H)]

currentColor = 1

for i in xrange(H):
	for j in xrange(W):
		if C[i][j] != '#' and color[i][j] == 0:
			q = Queue.Queue()
			q.put((i, j))
			color[i][j] = currentColor
			while not q.empty():
				y, x = q.get()
				for dy, dx in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
					ny = y + dy
					nx = x + dx
					if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
					if C[ny][nx] == '#': continue
					if color[ny][nx] != 0: continue
					color[ny][nx] = currentColor
					q.put((ny, nx))
			currentColor += 1

sy, sx, gy, gx = 0, 0, 0, 0
for i in xrange(H):
	for j in xrange(W):
		if color[i][j] == 1:
			sy, sx = i, j
		elif color[i][j] == 2:
			gy, gx = i, j

inf = 10 ** 10
dist = [[inf for _ in xrange(W)] for _ in xrange(H)]

dist[sy][sx] = 0
q = Queue.PriorityQueue()
q.put((0, sy, sx))

while not q.empty():
	_, y, x = q.get()
	for dy, dx in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
		ny = y + dy
		nx = x + dx
		if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
		if color[ny][nx] == 0:
			cost = 1
		else:
			cost = 0
		if dist[ny][nx] > dist[y][x] + cost:
			dist[ny][nx] = dist[y][x] + cost
			q.put((dist[ny][nx], ny, nx))

print dist[gy][gx]
0