結果
| 問題 | No.3504 Insert Maze |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 23:43:54 |
| 言語 | Python3 (3.14.3 + numpy 2.4.4 + scipy 1.17.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,443 bytes |
| 記録 | |
| コンパイル時間 | 501 ms |
| コンパイル使用メモリ | 20,664 KB |
| 実行使用メモリ | 171,384 KB |
| 最終ジャッジ日時 | 2026-04-18 23:44:30 |
| 合計ジャッジ時間 | 9,122 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 WA * 11 TLE * 1 -- * 67 |
ソースコード
import sys
from heapq import *
def solve():
data = sys.stdin.buffer.read().split()
H = int(data[0]); W = int(data[1])
grid = [data[i+2].decode() for i in range(H)]
INF = float('inf')
dist = {}
pq = []
def relax(s, c):
if dist.get(s, INF) > c:
dist[s] = c
heappush(pq, (c, s))
relax(('R', 0, 0, 1, 1), 0)
while pq:
d, s = heappop(pq)
if d > dist.get(s, INF): continue
t = s[0]
if t == 'R':
_, i, j, a, b = s
if i == H-1 and j == W-1:
print(d); return
for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
ni, nj = i+di, j+dj
if 0 <= ni < H and 0 <= nj < W and grid[ni][nj] != '#':
relax(('R', ni, nj, a, b), d+1)
if a:
if i > 0: relax(('VR', i-1, j, b), d+1)
if i < H-1: relax(('VR', i, j, b), d+1)
if b:
if j > 0: relax(('VC', i, j-1, a), d+1)
if j < W-1: relax(('VC', i, j, a), d+1)
elif t == 'VR':
_, r, j, b = s
if j > 0: relax(('VR', r, j-1, b), d+1)
if j < W-1: relax(('VR', r, j+1, b), d+1)
if grid[r][j] != '#': relax(('R', r, j, 0, b), d+1)
if grid[r+1][j] != '#': relax(('R', r+1, j, 0, b), d+1)
if b:
if j > 0: relax(('VX', r, j-1), d+1)
if j < W-1: relax(('VX', r, j ), d+1)
elif t == 'VC':
_, i, c, a = s
if i > 0: relax(('VC', i-1, c, a), d+1)
if i < H-1: relax(('VC', i+1, c, a), d+1)
if grid[i][c] != '#': relax(('R', i, c, a, 0), d+1)
if grid[i][c+1] != '#': relax(('R', i, c+1, a, 0), d+1)
if a:
if i > 0: relax(('VX', i-1, c), d+1)
if i < H-1: relax(('VX', i, c), d+1)
else: # VX
_, r, c = s
if c > 0: relax(('VX', r, c-1), d+1)
if c < W-1: relax(('VX', r, c+1), d+1)
if r > 0: relax(('VX', r-1, c), d+1)
if r < H-1: relax(('VX', r+1, c), d+1)
for er, ej in [(r, c), (r, c+1), (r+1, c), (r+1, c+1)]:
if 0 <= er < H and 0 <= ej < W and grid[er][ej] != '#':
relax(('R', er, ej, 0, 0), d+1)
print(-1)
solve()