結果

問題 No.157 2つの空洞
ユーザー nbisconbisco
提出日時 2017-02-03 23:35:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,929 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 8,532 KB
最終ジャッジ日時 2023-08-25 19:57:20
合計ジャッジ時間 1,595 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,412 KB
testcase_01 AC 17 ms
8,352 KB
testcase_02 AC 17 ms
8,460 KB
testcase_03 WA -
testcase_04 AC 16 ms
8,312 KB
testcase_05 AC 17 ms
8,428 KB
testcase_06 AC 17 ms
8,360 KB
testcase_07 AC 16 ms
8,220 KB
testcase_08 AC 17 ms
8,312 KB
testcase_09 AC 16 ms
8,400 KB
testcase_10 AC 17 ms
8,404 KB
testcase_11 WA -
testcase_12 AC 17 ms
8,220 KB
testcase_13 AC 17 ms
8,184 KB
testcase_14 WA -
testcase_15 AC 18 ms
8,092 KB
testcase_16 AC 16 ms
8,440 KB
testcase_17 WA -
testcase_18 AC 16 ms
8,360 KB
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def dist(start, goal):
    return abs(start[0]-goal[0]) + abs(start[1]-goal[1]) - 1


def gather_blanks(start_blank):
    blanks = set([(start_blank[0], start_blank[1])])
    queue = [(start_blank[0], start_blank[1])]
    while queue:
        cur = queue.pop(0)
        if cur[0] < (H-2) and wall[cur[0]+1][cur[1]] == ".":
            if not ((cur[0]+1, cur[1]) in blanks):
                queue.append((cur[0]+1, cur[1]))
                blanks.add((cur[0]+1, cur[1]))
        if cur[1] < (W-2) and wall[cur[0]][cur[1]+1] == ".":
            if not ((cur[0], cur[1]+1) in blanks):
                queue.append((cur[0], cur[1]+1))
                blanks.add((cur[0], cur[1]+1))
        if cur[0] > 2 and wall[cur[0]-1][cur[1]] == ".":
            if not ((cur[0]-1, cur[1]) in blanks):
                queue.append((cur[0]-1, cur[1]))
                blanks.add((cur[0]-1, cur[1]))
        if cur[1] > 2 and wall[cur[0]][cur[1]-1] == ".":
            if not ((cur[0], cur[1]-1) in blanks):
                queue.append((cur[0], cur[1]-1))
                blanks.add((cur[0], cur[1]-1))
    return blanks


W, H = map(int, input().split(" "))

wall = []
for i in range(H):
    wall.append(list(input()))

start_blank = [-1,-1]
for i in range(H):
    if wall[i].count(".") > 0:
        start_blank[0] = i
        start_blank[1] = wall[i].index(".")
        break
blanks_1 = gather_blanks(start_blank)

start_blank = [-1,-1]
for i in range(H):
    if wall[i].count(".") == 0:
        continue
    start_blank[0] = i
    for j in range(W):
        if wall[i][j] == "." and not ((i, j) in blanks_1):
            start_blank[1] = j
            break
    if start_blank[1] != -1:
        break

blanks_2 = gather_blanks(start_blank)

blanks_1 = list(blanks_1)
blanks_2 = list(blanks_2)

ans = max(H, W)
for b in range(len(blanks_1)):
    for s in range(len(blanks_2)):
        ans = min(ans, dist(blanks_1[b], blanks_2[s]))
print(ans)

0