Submission #18346808


Source Code Expand

import sys, os
import heapq as hq
import functools, collections
import math, random
from collections import Counter, defaultdict

inp = iter(sys.stdin.readlines())
input = lambda: next(inp)

nrows,_ = list(map(int,input().split()))

grid = []
for _ in range(nrows):
    grid.append(input().strip())
    
grid = ["#"*len(grid[0])] + grid + ["#"*len(grid[0])]
grid = [["#"] + list(row) + ["#"] for row in grid]

g = defaultdict(list)

for i in range(1,len(grid)-1):
    for j in range(1,len(grid[0])-1):
        val = grid[i][j]
        if val != ".":
            g[val].append((i,j))

start = g["S"][0]
end = g["G"][0]

if len(g) == 2:
    print(abs(start[0] - end[0]) + abs(start[1] - end[1]))
    sys.exit()

dxy = [(1,0),(-1,0),(0,-1),(0,1)]
visited = set([start])
stack = collections.deque([(start,0)])

while stack:
    (x,y),dist = stack.popleft()
    val = grid[x][y]

    for dx,dy in dxy:
        xx,yy = x+dx, y+dy
        if grid[xx][yy] == "#":
            continue
        if (xx,yy) in visited:
            continue
        visited.add((xx,yy))
        stack.append(((xx,yy), dist+1))

    if val in g:
        for i,j in g[val]:
            if (i,j) in visited:
                continue
            stack.append(((i,j), dist+1))
            visited.add((xx,yy))
        del g[val]

    if end in visited:
        break

if end in visited:
    while stack:
        loc, dist = stack.pop()
        if loc == end:
            print(dist)
            break
else:
    print(-1)

Submission Info

Submission Time
Task E - Third Avenue
User huikang
Language PyPy3 (7.3.0)
Score 500
Code Size 1556 Byte
Status AC
Exec Time 2333 ms
Memory 572020 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 38
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 569 ms 76484 KiB
random_01.txt AC 103 ms 76392 KiB
random_02.txt AC 99 ms 76364 KiB
random_03.txt AC 103 ms 76812 KiB
random_04.txt AC 103 ms 76520 KiB
random_05.txt AC 102 ms 76548 KiB
random_06.txt AC 104 ms 76520 KiB
random_07.txt AC 102 ms 76456 KiB
random_08.txt AC 101 ms 76432 KiB
random_09.txt AC 102 ms 76392 KiB
random_10.txt AC 102 ms 76396 KiB
random_11.txt AC 101 ms 76388 KiB
random_12.txt AC 104 ms 76792 KiB
random_13.txt AC 103 ms 76464 KiB
random_14.txt AC 103 ms 76800 KiB
random_15.txt AC 101 ms 76464 KiB
random_16.txt AC 162 ms 95888 KiB
random_17.txt AC 1067 ms 310416 KiB
random_18.txt AC 305 ms 156040 KiB
random_19.txt AC 147 ms 91176 KiB
random_20.txt AC 569 ms 240128 KiB
random_21.txt AC 136 ms 82588 KiB
random_22.txt AC 143 ms 85108 KiB
random_23.txt AC 355 ms 152640 KiB
random_24.txt AC 252 ms 140384 KiB
random_25.txt AC 253 ms 113496 KiB
random_26.txt AC 121 ms 79224 KiB
random_27.txt AC 393 ms 181172 KiB
random_28.txt AC 128 ms 81964 KiB
random_29.txt AC 116 ms 78124 KiB
random_30.txt AC 234 ms 127248 KiB
random_31.txt AC 2108 ms 548496 KiB
random_32.txt AC 2333 ms 537596 KiB
random_33.txt AC 410 ms 248108 KiB
random_34.txt AC 1865 ms 572020 KiB
sample_01.txt AC 113 ms 76352 KiB
sample_02.txt AC 99 ms 76636 KiB
sample_03.txt AC 98 ms 76388 KiB