Submission #18340539


Source Code Expand

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

# available on Google, not available on Codeforces
# import numpy as np
# import scipy

# def dijkstra_with_preprocessing(map_from_node_to_nodes_and_costs, source, target):

    # return costs[idxs[target]]


def dijkstra(G, s):
    n = len(G)
    visited = [False]*n
    weights = [math.inf]*n
    path = [None]*n
    queue = []
    weights[s] = 0
    hq.heappush(queue, (0, s))
    while len(queue) > 0:
        g, u = hq.heappop(queue)
        visited[u] = True
        for v, w in G[u]:
            if not visited[v]:
                f = g + w
                if f < weights[v]:
                    weights[v] = f
                    path[v] = u
                    hq.heappush(queue, (f, v))
    return path, weights


def console(*args):  
    # print on terminal in different color
    # print('\033[36m', *args, '\033[0m', file=sys.stderr)
    pass


ONLINE_JUDGE = False

# if Codeforces environment
# if os.path.exists('input.txt'):
#     ONLINE_JUDGE = True

# if ONLINE_JUDGE:
#     sys.stdin = open("input.txt","r")
#     sys.stdout = open("output.txt","w")

#     def console(*args):
#         pass


# def solve(*args):
#     # screen input
#     # if not ONLINE_JUDGE:
#     #     console("----- solving ------")
#     #     console(*args)
#     #     console("----- ------- ------")
#     return solve_(*args)


if True:
    # if memory is not a constraint
    inp = iter(sys.stdin.readlines())
    input = lambda: next(inp)
# else:
    # if memory is a constraint
# input = sys.stdin.readline


for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    nrows,_ = list(map(int,input().split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    grid = []
    for _ in range(nrows):
        grid.append(input().strip())
    #     grid.append(list(map(int,input().split())))

    # res = solve(grid)  # please change
    

    grid = ["#"*len(grid[0])] + grid + ["#"*len(grid[0])]
    grid = [["#"] + list(row) + ["#"] for row in grid]

    # locations
    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 == "#":
                continue
            if val != ".":
                # g[(i,j)].append((val,1))
                g[val].append((i,j))

    # print("check1")
    start = g["S"][0]
    end = g["G"][0]

    # console(g)
    # del grid

    c = Counter()
    dxy = [(1,0),(-1,0),(0,-1),(0,1)]
    visited = set([start])
    stack = collections.deque([(start,0)])
    while stack:
        (x,y),dist = stack.popleft()
        # print(dist)
        # c[dist] += 1
        val = grid[x][y]
        for dx,dy in dxy:
            xx,yy = x+dx, y+dy
            # print(x,dx, y,dy, xx,yy)
            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:
            # print(val)
            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]
            # print(g)
        if end in visited:
            break
    # print(c)
    # print("results")
    # print(visited)
    if end in visited:
        while stack:
            loc, dist = stack.pop()
            if loc == end:
                print(dist)
                break
    else:
        print(-1)



    # Codeforces - no case number required
    # print(res)
    # print(*res)  # if printing a list

Submission Info

Submission Time
Task E - Third Avenue
User huikang
Language PyPy3 (7.3.0)
Score 0
Code Size 4204 Byte
Status TLE
Exec Time 3325 ms
Memory 547256 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 37
TLE × 1
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 187 ms 76140 KiB
random_01.txt AC 118 ms 76496 KiB
random_02.txt AC 117 ms 76268 KiB
random_03.txt AC 117 ms 76248 KiB
random_04.txt AC 118 ms 76624 KiB
random_05.txt AC 116 ms 76504 KiB
random_06.txt AC 116 ms 76268 KiB
random_07.txt AC 120 ms 76348 KiB
random_08.txt AC 118 ms 76268 KiB
random_09.txt AC 116 ms 76668 KiB
random_10.txt AC 115 ms 76304 KiB
random_11.txt AC 119 ms 76304 KiB
random_12.txt AC 115 ms 76292 KiB
random_13.txt AC 121 ms 76624 KiB
random_14.txt AC 119 ms 76344 KiB
random_15.txt AC 118 ms 76544 KiB
random_16.txt AC 178 ms 91352 KiB
random_17.txt AC 1137 ms 249980 KiB
random_18.txt AC 338 ms 142340 KiB
random_19.txt AC 162 ms 86300 KiB
random_20.txt AC 529 ms 198392 KiB
random_21.txt AC 156 ms 81568 KiB
random_22.txt AC 162 ms 83516 KiB
random_23.txt AC 421 ms 148168 KiB
random_24.txt AC 294 ms 133940 KiB
random_25.txt AC 295 ms 111540 KiB
random_26.txt AC 137 ms 78652 KiB
random_27.txt AC 420 ms 165704 KiB
random_28.txt AC 152 ms 81444 KiB
random_29.txt AC 132 ms 78036 KiB
random_30.txt AC 283 ms 127188 KiB
random_31.txt AC 2359 ms 494000 KiB
random_32.txt AC 2602 ms 547256 KiB
random_33.txt TLE 3325 ms 521452 KiB
random_34.txt AC 2045 ms 510320 KiB
sample_01.txt AC 125 ms 76300 KiB
sample_02.txt AC 114 ms 76380 KiB
sample_03.txt AC 119 ms 76548 KiB