結果

問題 No.157 2つの空洞
ユーザー takakintakakin
提出日時 2020-05-04 11:16:32
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,441 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 8,588 KB
最終ジャッジ日時 2023-09-06 05:03:01
合計ジャッジ時間 1,602 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,420 KB
testcase_01 AC 17 ms
8,376 KB
testcase_02 AC 17 ms
8,408 KB
testcase_03 AC 18 ms
8,420 KB
testcase_04 AC 17 ms
8,488 KB
testcase_05 AC 17 ms
8,500 KB
testcase_06 AC 17 ms
8,572 KB
testcase_07 AC 17 ms
8,412 KB
testcase_08 AC 18 ms
8,492 KB
testcase_09 AC 17 ms
8,552 KB
testcase_10 AC 17 ms
8,396 KB
testcase_11 AC 17 ms
8,432 KB
testcase_12 AC 17 ms
8,492 KB
testcase_13 AC 18 ms
8,444 KB
testcase_14 AC 18 ms
8,540 KB
testcase_15 AC 19 ms
8,532 KB
testcase_16 AC 19 ms
8,480 KB
testcase_17 AC 19 ms
8,460 KB
testcase_18 AC 18 ms
8,588 KB
testcase_19 AC 19 ms
8,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
w,h=map(int,input().split())
C=[input() for _ in range(h)]
n=w*h
edge=[[] for i in range(n)]

import heapq
def dijkstra_heap(s):
  d=[float("inf")]*n
  used=[True]*n
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heapq.heappush(edgelist,e)
  while edgelist:
    minedge=heapq.heappop(edgelist)
    minedge_cost=minedge//(10**6)
    minedge_v=minedge%(10**6)
    if not used[minedge_v]:
      continue
    d[minedge_v] = minedge_cost
    used[minedge_v] = False
    for e in edge[minedge_v]:
      e_cost=e//(10**6)
      e_v=e%(10**6)
      if used[e_v]:
        heapq.heappush(edgelist,e+minedge_cost*10**6)
  return d
S=[]
for i in range(h):
  for j in range(w):
    if C[i][j]=="." and not S:
      S.append(i*w+j)
    if i!=0:
      if C[i-1][j]=="#":
        edge[i*w+j].append(10**6+(i-1)*w+j)
      else:
        edge[i*w+j].append((i-1)*w+j)
    if j!=w-1:
      if C[i][j+1]=="#":
        edge[i*w+j].append(10**6+i*w+j+1)
      else:
        edge[i*w+j].append(i*w+j+1)
    if i!=h-1:
      if C[i+1][j]=="#":
        edge[i*w+j].append(10**6+(i+1)*w+j)
      else:
        edge[i*w+j].append((i+1)*w+j)
    if j!=0:
      if C[i][j-1]=="#":
        edge[i*w+j].append(10**6+i*w+j-1)
      else:
        edge[i*w+j].append(i*w+j-1)

D=dijkstra_heap(S[0])
ans=0
for i in range(h):
  for j in range(w):
    if C[i][j]==".":
      ans=max(ans,D[i*w+j])
print(ans)
0