結果

問題 No.157 2つの空洞
ユーザー mkawa2mkawa2
提出日時 2019-12-26 21:49:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 676 ms / 2,000 ms
コード長 1,523 bytes
コンパイル時間 107 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 44,032 KB
最終ジャッジ日時 2024-04-15 10:40:23
合計ジャッジ時間 16,526 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 672 ms
43,896 KB
testcase_01 AC 668 ms
43,892 KB
testcase_02 AC 660 ms
43,760 KB
testcase_03 AC 668 ms
43,884 KB
testcase_04 AC 645 ms
44,032 KB
testcase_05 AC 640 ms
43,648 KB
testcase_06 AC 665 ms
43,904 KB
testcase_07 AC 665 ms
43,760 KB
testcase_08 AC 649 ms
43,900 KB
testcase_09 AC 658 ms
43,776 KB
testcase_10 AC 658 ms
43,772 KB
testcase_11 AC 674 ms
43,756 KB
testcase_12 AC 659 ms
43,636 KB
testcase_13 AC 653 ms
44,024 KB
testcase_14 AC 661 ms
43,904 KB
testcase_15 AC 650 ms
43,908 KB
testcase_16 AC 664 ms
43,904 KB
testcase_17 AC 657 ms
43,888 KB
testcase_18 AC 676 ms
43,764 KB
testcase_19 AC 647 ms
44,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import *
from bisect import *
from math import *
from collections import *
from heapq import *
from random import *
import numpy as np
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def MF(): return map(float, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LF(): return list(map(float, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij=[(1,0),(0,1),(-1,0),(0,-1)]

def main():
    def dfs(i,j,g):
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or ni>=h or nj<0 or nj>=w:
                continue
            if t[ni][nj]!=1:continue
            t[ni][nj]=0
            ij[g].append((ni,nj))
            dfs(ni,nj,g)

    ij=[[],[]]
    w,h=MI()
    t=[[(c==".")*1 for c in input()] for _ in range(h)]
    g=0
    for i in range(h):
        for j in range(w):
            if t[i][j]==1:
                t[i][j]=0
                ij[g].append((i,j))
                dfs(i,j,g)
                g+=1
            if g==2:break
        if g==2:break
    ans=100
    for i0,j0 in ij[0]:
        for i1,j1 in ij[1]:
            val=abs(i0-i1)+abs(j0-j1)-1
            if val<ans:ans=val
    print(ans)

main()
0