Submission #68756563


Source Code Expand

import bisect, heapq, sys, math, copy, itertools, decimal
from collections import defaultdict, deque
sys.setrecursionlimit(10**7)
def INT(): return int(input())
def MI(d=0): return map(lambda x:int(x)+d, input().split())
def MS(): return map(str, input().split())
def LI(d=0): return list(map(lambda x:int(x)+d, input().split()))
def LS(): return list(map(str, input().split()))
def pr_line(itr): print(*itr, sep='\n')
def pr_mtx(matrix): [print(*row) for row in matrix] 
dij = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dij2 = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
INF = float('inf')

H, W = MI()
A = [list(input()) for _ in range(H)]

visited = [[[INF, INF] for _ in range(W)] for _ in range(H)]

for i in range(H):
    for j in range(W):
        if A[i][j] == 'S':
            si, sj = i, j
        if A[i][j] == 'G':
            gi, gj = i, j

visited[si][sj][0] = 0
Q = deque()
Q.append((si, sj, 0))

while Q:
    i, j, k = Q.popleft()

    for di, dj in dij:
        ni, nj = i+di, j+dj
        if 0<=ni<H and 0<=nj<W and A[ni][nj] != '#':
            if k == 0:
                if A[ni][nj] in ('o', '.', 'S', 'G') and visited[ni][nj][k] == INF:
                    visited[ni][nj][k] = visited[i][j][k] + 1
                    Q.append((ni, nj, k))
                elif A[ni][nj] == '?' and visited[ni][nj][k^1] == INF:
                    visited[ni][nj][k^1] = visited[i][j][k] + 1
                    Q.append((ni, nj, k^1))
            else:
                if A[ni][nj] in ('x', '.', 'S','G')  and visited[ni][nj][k] == INF:
                    visited[ni][nj][k] = visited[i][j][k] + 1
                    Q.append((ni, nj, k))
                elif A[ni][nj] == '?' and visited[ni][nj][k^1] == INF:
                    visited[ni][nj][k^1] = visited[i][j][k] + 1
                    Q.append((ni, nj, k^1))

if min(visited[gi][gj]) != INF:
    print(min(visited[gi][gj]))
else:
    print(-1)

Submission Info

Submission Time
Task D - Toggle Maze
User BenKenobi
Language Python (PyPy 3.10-v7.3.12)
Score 400
Code Size 1984 Byte
Status AC
Exec Time 498 ms
Memory 146100 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 67
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 01_handmade_11.txt, 01_handmade_12.txt, 01_handmade_13.txt, 01_handmade_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 183 ms 94976 KiB
00_sample_01.txt AC 184 ms 94828 KiB
00_sample_02.txt AC 185 ms 95076 KiB
01_handmade_00.txt AC 417 ms 142560 KiB
01_handmade_01.txt AC 432 ms 142076 KiB
01_handmade_02.txt AC 424 ms 142392 KiB
01_handmade_03.txt AC 414 ms 142892 KiB
01_handmade_04.txt AC 184 ms 94796 KiB
01_handmade_05.txt AC 184 ms 94868 KiB
01_handmade_06.txt AC 187 ms 94916 KiB
01_handmade_07.txt AC 184 ms 94800 KiB
01_handmade_08.txt AC 183 ms 94720 KiB
01_handmade_09.txt AC 287 ms 131156 KiB
01_handmade_10.txt AC 303 ms 135996 KiB
01_handmade_11.txt AC 475 ms 142508 KiB
01_handmade_12.txt AC 436 ms 142844 KiB
01_handmade_13.txt AC 325 ms 135036 KiB
01_handmade_14.txt AC 320 ms 135560 KiB
02_random_00.txt AC 184 ms 95108 KiB
02_random_01.txt AC 362 ms 128152 KiB
02_random_02.txt AC 229 ms 98972 KiB
02_random_03.txt AC 384 ms 130312 KiB
02_random_04.txt AC 189 ms 95420 KiB
02_random_05.txt AC 430 ms 139848 KiB
02_random_06.txt AC 379 ms 130448 KiB
02_random_07.txt AC 412 ms 140048 KiB
02_random_08.txt AC 203 ms 103252 KiB
02_random_09.txt AC 400 ms 134976 KiB
02_random_10.txt AC 223 ms 127996 KiB
02_random_11.txt AC 422 ms 138480 KiB
02_random_12.txt AC 225 ms 128116 KiB
02_random_13.txt AC 269 ms 105456 KiB
02_random_14.txt AC 368 ms 129620 KiB
02_random_15.txt AC 233 ms 98684 KiB
02_random_16.txt AC 406 ms 140684 KiB
02_random_17.txt AC 231 ms 99040 KiB
02_random_18.txt AC 421 ms 140028 KiB
02_random_19.txt AC 264 ms 101452 KiB
02_random_20.txt AC 420 ms 140096 KiB
02_random_21.txt AC 191 ms 95112 KiB
02_random_22.txt AC 427 ms 140192 KiB
02_random_23.txt AC 238 ms 99044 KiB
02_random_24.txt AC 288 ms 121524 KiB
02_random_25.txt AC 220 ms 105372 KiB
02_random_26.txt AC 295 ms 112984 KiB
02_random_27.txt AC 254 ms 103660 KiB
02_random_28.txt AC 341 ms 133676 KiB
02_random_29.txt AC 274 ms 106436 KiB
02_random_30.txt AC 378 ms 134372 KiB
02_random_31.txt AC 252 ms 123072 KiB
02_random_32.txt AC 296 ms 129980 KiB
02_random_33.txt AC 285 ms 115836 KiB
02_random_34.txt AC 346 ms 134088 KiB
02_random_35.txt AC 273 ms 103500 KiB
02_random_36.txt AC 383 ms 122676 KiB
02_random_37.txt AC 319 ms 112428 KiB
02_random_38.txt AC 498 ms 146100 KiB
02_random_39.txt AC 443 ms 136104 KiB
02_random_40.txt AC 480 ms 145764 KiB
02_random_41.txt AC 209 ms 97468 KiB
02_random_42.txt AC 200 ms 97140 KiB
02_random_43.txt AC 208 ms 97388 KiB
02_random_44.txt AC 206 ms 97352 KiB
02_random_45.txt AC 204 ms 97356 KiB
02_random_46.txt AC 200 ms 97176 KiB
02_random_47.txt AC 207 ms 97396 KiB
02_random_48.txt AC 207 ms 97208 KiB