Submission #14488691


Source Code Expand

from collections import defaultdict
from heapq import *

# fast input
import sys
input = sys.stdin.buffer.readline
INF = 10 ** 10
# INF = float("inf")

try:
    profile
except:
    def profile(f): return f

DEBUG = False
if DEBUG:
    def dp(*x):  # debugprint
        print(*x)
else:
    def dp(*x): pass


@profile
def main():
    H, W, K = map(int, input().split())
    x1, y1, x2, y2 = map(int, input().split())
    mapdata = sys.stdin.buffer.read().splitlines()
    # print(data)
    start = (x1, y1)
    goal = (x2, y2)

    distances = defaultdict(lambda: (INF, 0))
    distances[(start, 0)] = (0, 0)
    shortest_path = {}
    shortest_path[(start, 0)] = None

    leaf = ord("@")

    queue = [((0, 0), (start, 0))]
    DIRS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    while queue:
        (d, frac), (frm, direction) = heappop(queue)
        # dp("(frm, direction)", (frm, direction))
        # dp("distances[(frm, direction)]", distances[(frm, direction)])
        if distances[(frm, direction)] < (d, frac):
            # already know shorter path
            continue
        if frm == goal:
            break

        x, y = frm
        # same direction
        # dp("direction", direction)
        dx, dy = DIRS[direction]
        nx = x + dx
        ny = y + dy
        if nx < 1 or H < nx or ny < 1 or W < ny:
            # dp("out of bounds")
            pass
        elif mapdata[nx - 1][ny - 1] == leaf:
            # dp("hit leaf")
            pass
        else:
            if frac == 0:
                newd = d + 1
                newfrac = -K + 1
            else:
                newd = d
                newfrac = frac + 1
            newdist = (newd, newfrac)
            to = ((nx, ny), direction)

            if distances[to] > newdist:
                # found shorter path
                distances[to] = newdist
                heappush(queue, (newdist, to))
                shortest_path[to] = frm

        for dir in range(4):
            if dir == direction:
                continue
            # dp("dir", dir)
            dx, dy = DIRS[dir]
            # dp("dx, dy", dx, dy)
            nx = x + dx
            ny = y + dy
            # dp("nx, ny", nx, ny)

            if nx < 1 or H < nx or ny < 1 or W < ny:
                # dp("out of bounds")
                continue
            dp(nx, ny, mapdata[nx - 1][ny - 1])
            if mapdata[nx - 1][ny - 1] == leaf:
                # dp("hit leaf")
                continue
            newdist = (d + 1, -K + 1)

            to = ((nx, ny), dir)
            if distances[to] > newdist:
                # found shorter path
                distances[to] = newdist
                heappush(queue, (newdist, to))
                shortest_path[to] = frm
        # dp("distances", distances)

    dist = min(distances[(goal, dir)] for dir in range(4))
    if dist[0] == INF:
        # unreachable
        print(-1)
    else:
        print(dist[0])

        # path = []
        # cur = goal
        # while True:
        #     frm = shortest_path[cur]
        #     path.append(frm)
        #     cur = frm
        #     if frm == start:
        #         break
        # path.reverse()
        # print(len(path))  # edge count
        # for (frm, to) in path:
        #     print(frm, to)


main()

Submission Info

Submission Time
Task F - Pond Skater
User nishiohirokazu
Language PyPy3 (7.3.0)
Score 0
Code Size 3412 Byte
Status TLE
Exec Time 3317 ms
Memory 375072 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 21
TLE × 11
Set Name Test Cases
Sample Sample_01.txt, Sample_02.txt, Sample_03.txt
All Sample_01.txt, Sample_02.txt, Sample_03.txt, killer_01.txt, killer_02.txt, killer_03.txt, killer_04.txt, killer_05.txt, ng_large_01.txt, ng_large_02.txt, ng_large_03.txt, ng_small_01.txt, ng_small_02.txt, ng_small_03.txt, path_01.txt, path_02.txt, path_03.txt, path_04.txt, path_05.txt, rand_1000_01.txt, rand_1000_02.txt, rand_1000_03.txt, rand_20_01.txt, rand_20_02.txt, rand_20_03.txt, rand_300_01.txt, rand_300_02.txt, rand_300_03.txt, rand_small_01.txt, rand_small_02.txt, rand_small_03.txt, superkiller_01.txt
Case Name Status Exec Time Memory
Sample_01.txt AC 71 ms 65632 KiB
Sample_02.txt AC 62 ms 65244 KiB
Sample_03.txt AC 67 ms 65504 KiB
killer_01.txt TLE 3316 ms 316968 KiB
killer_02.txt TLE 3315 ms 290884 KiB
killer_03.txt AC 2340 ms 242620 KiB
killer_04.txt TLE 3315 ms 304472 KiB
killer_05.txt AC 1224 ms 218976 KiB
ng_large_01.txt AC 83 ms 71744 KiB
ng_large_02.txt AC 69 ms 71796 KiB
ng_large_03.txt AC 66 ms 71440 KiB
ng_small_01.txt AC 57 ms 65312 KiB
ng_small_02.txt AC 65 ms 65416 KiB
ng_small_03.txt AC 66 ms 65504 KiB
path_01.txt AC 62 ms 73960 KiB
path_02.txt AC 64 ms 72976 KiB
path_03.txt AC 780 ms 236900 KiB
path_04.txt AC 834 ms 235076 KiB
path_05.txt AC 2287 ms 338620 KiB
rand_1000_01.txt TLE 3314 ms 285224 KiB
rand_1000_02.txt TLE 3313 ms 288980 KiB
rand_1000_03.txt TLE 3315 ms 283564 KiB
rand_20_01.txt TLE 3317 ms 375072 KiB
rand_20_02.txt AC 210 ms 80404 KiB
rand_20_03.txt TLE 3315 ms 284880 KiB
rand_300_01.txt TLE 3314 ms 321812 KiB
rand_300_02.txt TLE 3315 ms 283600 KiB
rand_300_03.txt TLE 3314 ms 322192 KiB
rand_small_01.txt AC 72 ms 65224 KiB
rand_small_02.txt AC 63 ms 65980 KiB
rand_small_03.txt AC 61 ms 65896 KiB
superkiller_01.txt AC 560 ms 211492 KiB