結果

問題 No.3063 幅優先探索
ユーザー mkawa2mkawa2
提出日時 2020-04-01 22:17:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,230 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,064 KB
実行使用メモリ 54,964 KB
最終ジャッジ日時 2023-09-09 18:40:35
合計ジャッジ時間 2,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 WA -
testcase_06 RE -
testcase_07 AC 1,216 ms
54,964 KB
testcase_08 RE -
testcase_09 AC 21 ms
8,652 KB
testcase_10 AC 19 ms
8,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

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

def main():
    h,w=MI()
    si,sj=MI()
    gi,gj=MI()
    tt=[]
    for _ in range(h):
        row=[False]+[c=="#" for c in SI()]+[False]
        tt.append(row)
    q=deque()
    q.append((si,sj,0))
    dist=[[-1]*(w+2) for _ in range(h+2)]
    while q:
        i,j,d=q.popleft()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni==gi and nj==gj:
                print(d+1)
                exit()
            if ni<0 or ni>h+1 or nj<0 or nj>w+1:continue
            if tt[ni][nj]:continue
            if dist[ni][nj]!=-1:continue
            dist[ni][nj]=d+1
            q.append((ni,nj,d+1))

main()
0