結果

問題 No.1572 XI
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-07-02 10:43:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 877 ms / 2,000 ms
コード長 1,449 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 161,048 KB
最終ジャッジ日時 2023-09-11 06:23:09
合計ジャッジ時間 15,532 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
71,380 KB
testcase_01 AC 95 ms
71,560 KB
testcase_02 AC 95 ms
71,448 KB
testcase_03 AC 95 ms
71,312 KB
testcase_04 AC 203 ms
88,736 KB
testcase_05 AC 512 ms
116,864 KB
testcase_06 AC 415 ms
118,612 KB
testcase_07 AC 139 ms
77,908 KB
testcase_08 AC 149 ms
101,344 KB
testcase_09 AC 281 ms
94,312 KB
testcase_10 AC 214 ms
98,348 KB
testcase_11 AC 154 ms
79,076 KB
testcase_12 AC 243 ms
88,940 KB
testcase_13 AC 159 ms
79,272 KB
testcase_14 AC 223 ms
89,272 KB
testcase_15 AC 134 ms
78,788 KB
testcase_16 AC 209 ms
86,500 KB
testcase_17 AC 136 ms
78,152 KB
testcase_18 AC 136 ms
87,288 KB
testcase_19 AC 195 ms
104,948 KB
testcase_20 AC 178 ms
94,008 KB
testcase_21 AC 424 ms
119,932 KB
testcase_22 AC 496 ms
116,468 KB
testcase_23 AC 131 ms
77,820 KB
testcase_24 AC 98 ms
71,364 KB
testcase_25 AC 98 ms
71,440 KB
testcase_26 AC 101 ms
72,044 KB
testcase_27 AC 96 ms
71,352 KB
testcase_28 AC 96 ms
71,404 KB
testcase_29 AC 97 ms
71,496 KB
testcase_30 AC 104 ms
76,836 KB
testcase_31 AC 95 ms
71,452 KB
testcase_32 AC 95 ms
71,496 KB
testcase_33 AC 95 ms
71,336 KB
testcase_34 AC 198 ms
125,060 KB
testcase_35 AC 167 ms
123,012 KB
testcase_36 AC 324 ms
131,576 KB
testcase_37 AC 845 ms
160,752 KB
testcase_38 AC 565 ms
145,916 KB
testcase_39 AC 432 ms
139,028 KB
testcase_40 AC 231 ms
123,452 KB
testcase_41 AC 566 ms
144,396 KB
testcase_42 AC 877 ms
161,048 KB
testcase_43 AC 417 ms
137,172 KB
testcase_44 AC 219 ms
129,452 KB
testcase_45 AC 663 ms
152,948 KB
testcase_46 AC 660 ms
152,008 KB
testcase_47 AC 135 ms
126,216 KB
testcase_48 AC 556 ms
148,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w=map(int,input().split())
s=list(map(int,input().split()))
g=list(map(int,input().split()))
a=[input() for _ in range(h)]
s[0],s[1]=s[0]-1,s[1]-1
g[0],g[1]=g[0]-1,g[1]-1
seen=[-1]*(h*w*6)
# seen[(i*h+j)*6+x]:マス(i,j)でさいころの状態がx
# x=0:1が上,
# x=1:1が右側(wの+方向),
# x=2:1が左側(wの-方向),
# x=3:1が奥側(hの+方向),
# x=4:1が手前側(hの-方向),
# x=5:1が下
trans=lambda i,j,x:(i*w+j)*6+x
seen[trans(s[0],s[1],0)]=0
d=[[[0]*6 for _ in range(3)] for _ in range(3)]
# 1上の状態から
d[0][1][0]=1
d[0][-1][0]=2
d[1][0][0]=3
d[-1][0][0]=4
# 1右の状態から
d[0][1][1]=5
d[0][-1][1]=0
d[1][0][1]=1
d[-1][0][1]=1
# 1左の状態から
d[0][1][2]=0
d[0][-1][2]=5
d[1][0][2]=2
d[-1][0][2]=2
# 1奥の状態から
d[0][1][3]=3
d[0][-1][3]=3
d[1][0][3]=5
d[-1][0][3]=0
# 1手前の状態から
d[0][1][4]=4
d[0][-1][4]=4
d[1][0][4]=0
d[-1][0][4]=5
# 1下の状態から
d[0][1][5]=2
d[0][-1][5]=1
d[1][0][5]=4
d[-1][0][5]=3

from collections import deque
todo=deque([[s[0],s[1],0]])
while todo:
  i,j,x=todo.popleft()
  for di,dj in ((0,1),(0,-1),(1,0),(-1,0)):
    ni,nj=i+di,j+dj
    if not 0<=ni<h:continue
    if not 0<=nj<w:continue
    if a[ni][nj]=="#":continue
    nx=d[di][dj][x]
    if seen[trans(ni,nj,nx)]==-1:
      seen[trans(ni,nj,nx)]=seen[trans(i,j,x)]+1
      todo.append([ni,nj,nx])
      if (ni,nj,nx)==(g[0],g[1],0):
        print(seen[trans(ni,nj,nx)])
        exit()
print(-1)

0