結果
| 問題 | No.3504 Insert Maze |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 13:15:30 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,869 bytes |
| 記録 | |
| コンパイル時間 | 2,138 ms |
| コンパイル使用メモリ | 84,992 KB |
| 実行使用メモリ | 308,096 KB |
| 最終ジャッジ日時 | 2026-04-18 13:16:52 |
| 合計ジャッジ時間 | 66,652 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 79 WA * 6 |
ソースコード
import sys
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#####################################################################
h, w = na()
s = [input() for i in range(h)]
from collections import deque
for i in range(h):
for j in range(w):
if s[i][j] == "S":
sx, sy = i, j
elif s[i][j] == "G":
gx, gy = i, j
def f(x, y, z):
return x * 3 * w + y * 3 + z
dist = [-1] * (h * w * 3)
dq = deque([(sx, sy, 0)])
dist[f(sx, sy, 0)] = 0
while dq:
x, y, i = dq.popleft()
if i == 0:
for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
nx = x + dx
ny = y + dy
if not (0 <= nx < h and 0 <= ny < w):
continue
if s[nx][ny] != "#" and dist[f(nx, ny, 0)] == -1:
dist[f(nx, ny, 0)] = dist[f(x, y, 0)] + 1
dq.append((nx, ny, 0))
for dx, dy in [[1, 0], [-1, 0]]:
nx = x + dx
ny = y + dy
if not (0 <= nx < h and 0 <= ny < w):
continue
if dist[f(nx, ny, 1)] == -1:
dist[f(nx, ny, 1)] = dist[f(x, y, 0)] + 1
dq.append((nx, ny, 1))
for dx, dy in [[0, 1], [0, -1]]:
nx = x + dx
ny = y + dy
if not (0 <= nx < h and 0 <= ny < w):
continue
if dist[f(nx, ny, 2)] == -1:
dist[f(nx, ny, 2)] = dist[f(x, y, 0)] + 1
dq.append((nx, ny, 2))
elif i == 1:
for dx, dy in [[0, 1], [0, -1]]:
nx = x + dx
ny = y + dy
if not (0 <= nx < h and 0 <= ny < w):
continue
if dist[f(nx, ny, 1)] == -1:
dist[f(nx, ny, 1)] = dist[f(x, y, i)] + 1
dq.append((nx, ny, 1))
if dist[f(x, y, 0)] == -1 and s[x][y] != "#":
dist[f(x, y, 0)] = dist[f(x, y, i)] + 1
dq.append((x, y, 0))
else:
for dx, dy in [[1, 0], [-1, 0]]:
nx = x + dx
ny = y + dy
if not (0 <= nx < h and 0 <= ny < w):
continue
if dist[f(nx, ny, 2)] == -1:
dist[f(nx, ny, 2)] = dist[f(x, y, i)] + 1
dq.append((nx, ny, 2))
if dist[f(x, y, 0)] == -1 and s[x][y] != "#":
dist[f(x, y, 0)] = dist[f(x, y, i)] + 1
dq.append((x, y, 0))
# for i in range(h):
# print([dist[f(i, j, 0)] for j in range(w)])
# print()
# for i in range(h):
# print([dist[f(i, j, 1)] for j in range(w)])
# print()
# for i in range(h):
# print([dist[f(i, j, 2)] for j in range(w)])
print(dist[f(gx, gy, 0)])