結果
| 問題 |
No.2096 Rage With Our Friends
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-14 21:30:24 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,078 bytes |
| コンパイル時間 | 517 ms |
| コンパイル使用メモリ | 82,528 KB |
| 実行使用メモリ | 236,576 KB |
| 最終ジャッジ日時 | 2024-12-15 12:26:44 |
| 合計ジャッジ時間 | 21,890 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 4 WA * 23 |
ソースコード
import sys
from collections import *
input = lambda :sys.stdin.readline().rstrip()
"""
考察の内容
01-BFSでよさそうだけどEを持たなきゃいけなさそう?
→前進するとE=0になるので後退→前進の2回分を全探索でよさげ
後退については一番後退せずにすむ点にジャンプするのが得(この後後退する場合もできるだけ前のほうが候補が増える)
前進についてはできるだけ前にだけいった後、好きなだけbackできるように頂点倍化?
"""
H,W = map(int,input().split())
_,sy,_,gy = map(int,input().split())
sy,gy = sy-1,gy-1
S = [input() for i in range(H)]
mini = [[H for j in range(W)] for i in range(H)]
for j in range(W):
for i in range(H)[::-1]:
if i!=H-1:
mini[i][j] = min(mini[i][j],mini[i+1][j])
if S[i][j]=="#":
mini[i][j] = i
INF = 10**17
"""
p=1のとき同じ列でback可能
"""
dist = [[INF for j in range(W)] for i in range(H)]
dist[H-1][sy] = 0
deq = deque([(H-1,sy)])
done = [[False]*W for i in range(H)]
while deq:
h,w = deq.popleft()
if done[h][w]:
continue
done[h][w] = True
"""
前進する
"""
if h!=0:
if w!=0 and mini[h-1][w-1]!=H:
nh = mini[h-1][w-1]
if dist[nh][w-1] > dist[h][w]:
dist[nh][w-1] = dist[h][w]
deq.appendleft((nh,w-1))
if w!=W-1 and mini[h-1][w+1]!=H:
nh = mini[h-1][w+1]
if dist[nh][w+1] > dist[h][w]:
dist[nh][w+1] = dist[h][w]
deq.appendleft((nh,w+1))
"""
後退する
"""
if w!=0 and mini[h][w-1]!=H:
nh,nw = mini[h][w-1],w-1
E = nh-h
"""
ここからさらに前進する
"""
if nw!=0 and mini[max(0,nh-1-E//2)][nw-1]!=H:
nnh,nnw = mini[max(0,nh-1-E//2)][nw-1],nw-1
if dist[nnh][nnw] > dist[h][w]:
dist[nnh][nnw] = dist[h][w]
deq.appendleft((nnh,nnw))
if nw!=W-1 and mini[max(0,nh-1-E//2)][nw+1]!=H:
nnh,nnw = mini[max(0,nh-1-E//2)][nw+1],nw+1
if dist[nnh][nnw] > dist[h][w]:
dist[nnh][nnw] = dist[h][w]
deq.appendleft((nnh,nnw))
"""
ブースト
"""
if nw!=0 and mini[max(0,nh-1-E)][nw-1]!=H:
nnh,nnw = mini[max(0,nh-1-E)][nw-1],nw-1
if dist[nnh][nnw] > dist[h][w] + 1:
dist[nnh][nnw] = dist[h][w] + 1
deq.append((nnh,nnw))
if nw!=W-1 and mini[max(0,nh-1-E)][nw+1]!=H:
nnh,nnw = mini[max(0,nh-1-E)][nw+1],nw+1
if dist[nnh][nnw] > dist[h][w] + 1:
dist[nnh][nnw] = dist[h][w] + 1
deq.append((nnh,nnw))
if w!=W-1 and mini[h][w+1]!=H:
nh,nw = mini[h][w+1],w+1
E = nh-h
"""
ここからさらに前進する
"""
if nw!=0 and mini[max(0,nh-1-E//2)][nw-1]!=H:
nnh,nnw = mini[max(0,nh-1-E//2)][nw-1],nw-1
if dist[nnh][nnw] > dist[h][w]:
dist[nnh][nnw] = dist[h][w]
deq.appendleft((nnh,nnw))
if nw!=W-1 and mini[max(0,nh-1-E//2)][nw+1]!=H:
nnh,nnw = mini[max(0,nh-1-E//2)][nw+1],nw+1
if dist[nnh][nnw] > dist[h][w]:
dist[nnh][nnw] = dist[h][w]
deq.appendleft((nnh,nnw))
"""
ブースト
"""
if nw!=0 and mini[max(0,nh-1-E)][nw-1]!=H:
nnh,nnw = mini[max(0,nh-1-E)][nw-1],nw-1
if dist[nnh][nnw] > dist[h][w] + 1:
dist[nnh][nnw] = dist[h][w] + 1
deq.append((nnh,nnw))
if nw!=W-1 and mini[max(0,nh-1-E)][nw+1]!=H:
nnh,nnw = mini[max(0,nh-1-E)][nw+1],nw+1
if dist[nnh][nnw] > dist[h][w] + 1:
dist[nnh][nnw] = dist[h][w] + 1
deq.append((nnh,nnw))
if dist[0][gy] == INF:
print(-1)
else:
print(dist[0][gy])