結果

問題 No.340 雪の足跡
ユーザー mkawa2mkawa2
提出日時 2020-04-29 01:39:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,945 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 95,076 KB
最終ジャッジ日時 2023-08-17 12:36:50
合計ジャッジ時間 18,335 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,720 KB
testcase_01 AC 18 ms
8,732 KB
testcase_02 AC 19 ms
8,740 KB
testcase_03 AC 18 ms
8,796 KB
testcase_04 AC 18 ms
8,748 KB
testcase_05 AC 17 ms
8,696 KB
testcase_06 AC 17 ms
8,640 KB
testcase_07 AC 17 ms
8,780 KB
testcase_08 AC 17 ms
8,592 KB
testcase_09 AC 18 ms
8,804 KB
testcase_10 AC 18 ms
8,660 KB
testcase_11 AC 23 ms
8,700 KB
testcase_12 AC 21 ms
8,780 KB
testcase_13 AC 23 ms
8,828 KB
testcase_14 AC 102 ms
10,328 KB
testcase_15 AC 114 ms
10,388 KB
testcase_16 AC 73 ms
9,496 KB
testcase_17 AC 139 ms
10,920 KB
testcase_18 AC 125 ms
10,640 KB
testcase_19 AC 139 ms
10,912 KB
testcase_20 AC 876 ms
38,688 KB
testcase_21 AC 403 ms
8,892 KB
testcase_22 AC 181 ms
32,020 KB
testcase_23 TLE -
testcase_24 AC 695 ms
32,600 KB
testcase_25 AC 844 ms
47,780 KB
testcase_26 AC 125 ms
9,520 KB
testcase_27 AC 38 ms
8,908 KB
testcase_28 AC 128 ms
9,924 KB
testcase_29 TLE -
testcase_30 AC 759 ms
21,088 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
import sys

sys.setrecursionlimit(10 ** 6)

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 main():
    w,h,n=MI()
    if (w,h)==(1,1):
        print(0)
        exit()
    # lrは左右移動、udは上下移動。いもす法の要領で移動可能な区間を記録していく
    lr=[0]*(w*h+1)
    ud=[0]*(w*h+1)
    for _ in range(n):
        SI()
        bb=LI()
        # 区間の始点に+1、終点に-1をしていく
        for b0,b1 in zip(bb,bb[1:]):
            if b0>b1:b0,b1=b1,b0
            # 左右移動の場合
            if b0//w==b1//w:
                lr[b0+1]+=1
                lr[b1+1]-=1
            # 上下移動の場合
            else:
                j0=b0%w*h+b0//w
                j1=b1%w*h+b1//w
                ud[j0+1]+=1
                ud[j1+1]-=1
    # 累積和をとって移動可能な区間を求める
    for i in range(w*h):lr[i+1]+=lr[i]
    for j in range(w*h):ud[j+1]+=ud[j]
    #print(lr)
    #print(ud)

    q=deque()
    q.append((0,0))
    first=[True]*w*h

    def finish(d):
        print(d+1)
        exit()

    while q:
        d,i=q.popleft()
        j=i%w*h+i//w
        # 左への移動
        if lr[i] and first[i-1]:
            if i-1==h*w-1:finish(d)
            first[i-1]=False
            q.append((d+1,i-1))
        # 右への移動
        if lr[i+1] and first[i+1]:
            if i+1==h*w-1:finish(d)
            first[i+1]=False
            q.append((d+1,i+1))
        # 上への移動
        if ud[j+1] and first[i+w]:
            if i+w==h*w-1:finish(d)
            first[i+w]=False
            q.append((d+1,i+w))
        # 下への移動
        if ud[j] and first[i-w]:
            if i-w==h*w-1:finish(d)
            first[i-w]=False
            q.append((d+1,i-w))

    print("Odekakedekinai..")

main()
0