結果

問題 No.157 2つの空洞
ユーザー lllllll88938494lllllll88938494
提出日時 2022-05-23 22:06:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,742 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 12,036 KB
実行使用メモリ 10,372 KB
最終ジャッジ日時 2023-10-20 17:45:00
合計ジャッジ時間 1,893 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,316 KB
testcase_01 AC 31 ms
10,316 KB
testcase_02 AC 30 ms
10,316 KB
testcase_03 AC 30 ms
10,316 KB
testcase_04 AC 30 ms
10,316 KB
testcase_05 AC 30 ms
10,348 KB
testcase_06 AC 30 ms
10,368 KB
testcase_07 AC 30 ms
10,368 KB
testcase_08 AC 29 ms
10,368 KB
testcase_09 AC 30 ms
10,368 KB
testcase_10 AC 30 ms
10,368 KB
testcase_11 AC 30 ms
10,368 KB
testcase_12 AC 30 ms
10,368 KB
testcase_13 AC 30 ms
10,372 KB
testcase_14 AC 31 ms
10,372 KB
testcase_15 AC 30 ms
10,372 KB
testcase_16 AC 30 ms
10,372 KB
testcase_17 AC 31 ms
10,372 KB
testcase_18 AC 31 ms
10,372 KB
testcase_19 AC 30 ms
10,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
w,h =map(int,input().split())
grid=[input() for i in range(h)]
dist=[[0]*w for i in range(h)]
l=[1,0,-1,0]
r=[0,1,0,-1]

def  bfs(x,dq):
    while dq:
        ll,rr=dq.popleft()
        for i in range(4):
            t1 = ll+l[i]
            t2 = rr+r[i]
            if 0 <= t1 < h and 0 <= t2 < w:
                if grid[t1][t2] == '.':
                    if dist[t1][t2] == 0:
                        dist[t1][t2] = x
                        dq.append([t1,t2])
                        
            
def s(x):
    for i in range(h):
        for j in range(w):
            if grid[i][j] == '.':
                if dist[i][j] == 0:
                    de=deque()
                    de.append([i,j])
                    dist[i][j]=x
                    bfs(x,de)
                    return [i,j]
          
one=s(1)
s(2)

cost=[[1000]*w for i in range(h)]
cost[one[0]][one[1]] = 0
            
def  bfs2(x):
    ans = 1000
    de = deque()
    de.append(x)
    while de:
        ll,rr = de.popleft()
        for i in range(4):
            t1 = ll+l[i]
            t2 = rr+r[i]
            if 0 <= t1 < h and 0 <= t2 < w:
                if dist[t1][t2] == 1:
                    if cost[t1][t2] > cost[ll][rr]:
                        de.append([t1,t2])
                        cost[t1][t2] = cost[ll][rr]
                elif dist[t1][t2] == 0:
                    if cost[t1][t2] > cost[ll][rr]+1:
                        cost[t1][t2] = cost[ll][rr]+1
                        de.append([t1,t2])
                        
                elif  dist[t1][t2] == 2:
                    ans = min(ans,cost[ll][rr])
    return ans
                    
print(bfs2(one))
                            

    
            
0