結果

問題 No.1572 XI
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-07-02 10:42:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 875 ms / 2,000 ms
コード長 1,485 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 158,660 KB
最終ジャッジ日時 2023-09-11 06:22:24
合計ジャッジ時間 15,699 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,100 KB
testcase_01 AC 96 ms
71,204 KB
testcase_02 AC 97 ms
70,940 KB
testcase_03 AC 96 ms
71,232 KB
testcase_04 AC 204 ms
89,040 KB
testcase_05 AC 499 ms
117,220 KB
testcase_06 AC 403 ms
119,016 KB
testcase_07 AC 135 ms
77,704 KB
testcase_08 AC 140 ms
101,176 KB
testcase_09 AC 265 ms
93,960 KB
testcase_10 AC 204 ms
97,736 KB
testcase_11 AC 150 ms
79,060 KB
testcase_12 AC 233 ms
88,852 KB
testcase_13 AC 152 ms
79,636 KB
testcase_14 AC 214 ms
89,152 KB
testcase_15 AC 131 ms
79,148 KB
testcase_16 AC 196 ms
85,488 KB
testcase_17 AC 128 ms
77,652 KB
testcase_18 AC 126 ms
87,596 KB
testcase_19 AC 185 ms
104,192 KB
testcase_20 AC 168 ms
93,656 KB
testcase_21 AC 411 ms
119,832 KB
testcase_22 AC 489 ms
116,784 KB
testcase_23 AC 128 ms
77,576 KB
testcase_24 AC 96 ms
71,460 KB
testcase_25 AC 96 ms
71,456 KB
testcase_26 AC 98 ms
72,184 KB
testcase_27 AC 94 ms
71,316 KB
testcase_28 AC 95 ms
71,324 KB
testcase_29 AC 95 ms
71,296 KB
testcase_30 AC 104 ms
76,844 KB
testcase_31 AC 94 ms
71,480 KB
testcase_32 AC 94 ms
71,312 KB
testcase_33 AC 95 ms
71,340 KB
testcase_34 AC 176 ms
120,788 KB
testcase_35 AC 149 ms
120,400 KB
testcase_36 AC 310 ms
128,764 KB
testcase_37 AC 839 ms
158,660 KB
testcase_38 AC 542 ms
143,424 KB
testcase_39 AC 423 ms
136,140 KB
testcase_40 AC 215 ms
120,936 KB
testcase_41 AC 542 ms
141,552 KB
testcase_42 AC 875 ms
158,468 KB
testcase_43 AC 393 ms
132,424 KB
testcase_44 AC 201 ms
127,036 KB
testcase_45 AC 625 ms
150,888 KB
testcase_46 AC 639 ms
149,504 KB
testcase_47 AC 118 ms
120,104 KB
testcase_48 AC 531 ms
144,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
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