結果

問題 No.157 2つの空洞
ユーザー nbisconbisco
提出日時 2017-02-03 23:50:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,920 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 11,136 KB
実行使用メモリ 8,180 KB
最終ジャッジ日時 2023-08-25 19:58:19
合計ジャッジ時間 1,260 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,092 KB
testcase_01 AC 16 ms
8,040 KB
testcase_02 AC 16 ms
8,040 KB
testcase_03 AC 17 ms
8,092 KB
testcase_04 AC 17 ms
8,028 KB
testcase_05 AC 16 ms
8,180 KB
testcase_06 AC 17 ms
8,020 KB
testcase_07 AC 15 ms
8,076 KB
testcase_08 AC 15 ms
8,076 KB
testcase_09 AC 16 ms
8,052 KB
testcase_10 AC 16 ms
8,028 KB
testcase_11 AC 15 ms
8,168 KB
testcase_12 AC 16 ms
8,080 KB
testcase_13 AC 15 ms
8,036 KB
testcase_14 AC 16 ms
8,032 KB
testcase_15 AC 17 ms
8,080 KB
testcase_16 AC 16 ms
8,088 KB
testcase_17 AC 17 ms
8,096 KB
testcase_18 AC 15 ms
8,016 KB
testcase_19 AC 16 ms
8,040 KB
権限があれば一括ダウンロードができます

ソースコード

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] > 1 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] > 1 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]
ok = False
for i in range(H):
    for j in range(W):
        if (i, j) in blanks_1:
            continue
        if wall[i][j] == ".":
            start_blank[0] = i
            start_blank[1] = j
            ok = True
            break
    if ok:
        break

blanks_2 = gather_blanks(start_blank)

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

ans = 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