提出 #74864431


ソースコード 拡げる

import sys
from collections import deque

input = lambda: sys.stdin.readline().rstrip()

delta = [1, -1, 0, 0]
direction = "DULR"

INF = int(5e6)


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]

    def invalid(r, c):
        return r < 0 or r >= H or c < 0 or c >= W

    dest = (0, 0)
    start = (0, 0)
    for i in range(H):
        for j in range(W):
            if S[i][j] == "S":
                start = (i, j)
            elif S[i][j] == "G":
                dest = (i, j)

    time = [[[-1] * 4 for _ in range(W)] for _ in range(H)]
    prv = [[[(0,0)] * 4 for _ in range(W)] for _ in range(H)]

    time[start[0]][start[1]] = [0] * 4
    queue = deque([(start[0], start[1], -1)])
    while queue:
        r, c, d = queue.popleft()

        if time[r][c][d] > INF:
            continue

        if S[r][c] == "G":
            break

        for k in range(4):
            if S[r][c] == "x" and k == d:
                continue
            elif S[r][c] == "o" and k != d:
                continue

            nr = r + delta[k]
            nc = c + delta[3 - k]

            if invalid(nr, nc) or S[nr][nc] == "#":
                continue
            if time[nr][nc][k] != -1:
                continue

            time[nr][nc][k] = time[r][c][d] + 1
            prv[nr][nc][k] = (k, d)

            queue.append((nr, nc, k))

    d = -1
    for i in range(4):
        if time[dest[0]][dest[1]][i] != -1:
            d = i

    if d == -1:
        print("No")
        return

    ans = []
    r, c = dest
    while (r, c) != start:
        d, prvd = prv[r][c][d]
        ans.append(direction[d])

        r, c = r - delta[d], c - delta[3 - d]
        d = prvd

    print("Yes")
    print("".join(reversed(ans)))


main()

提出情報

提出日時
問題 D - Go Straight
ユーザ skuru
言語 Python (PyPy 3.11-v7.3.20)
得点 425
コード長 1852 Byte
結果 AC
実行時間 1804 ms
メモリ 586112 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 425 / 425
結果
AC × 3
AC × 46
セット名 テストケース
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.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
ケース名 結果 実行時間 メモリ
example_00.txt AC 73 ms 94240 KiB
example_01.txt AC 74 ms 94248 KiB
example_02.txt AC 76 ms 94216 KiB
hand_00.txt AC 1654 ms 453272 KiB
hand_01.txt AC 253 ms 269380 KiB
hand_02.txt AC 1804 ms 586112 KiB
hand_03.txt AC 1794 ms 585592 KiB
hand_04.txt AC 74 ms 94080 KiB
hand_05.txt AC 1619 ms 447448 KiB
hand_06.txt AC 613 ms 456212 KiB
hand_07.txt AC 1634 ms 452896 KiB
hand_08.txt AC 240 ms 269212 KiB
hand_09.txt AC 73 ms 94216 KiB
random_00.txt AC 242 ms 269244 KiB
random_01.txt AC 1128 ms 367788 KiB
random_02.txt AC 460 ms 304088 KiB
random_03.txt AC 245 ms 269340 KiB
random_04.txt AC 668 ms 335416 KiB
random_05.txt AC 754 ms 321348 KiB
random_06.txt AC 243 ms 269192 KiB
random_07.txt AC 245 ms 269616 KiB
random_08.txt AC 418 ms 312256 KiB
random_09.txt AC 626 ms 337672 KiB
random_10.txt AC 633 ms 309704 KiB
random_11.txt AC 685 ms 322848 KiB
random_12.txt AC 780 ms 331172 KiB
random_13.txt AC 241 ms 269984 KiB
random_14.txt AC 557 ms 315040 KiB
random_15.txt AC 248 ms 269960 KiB
random_16.txt AC 245 ms 269476 KiB
random_17.txt AC 251 ms 270324 KiB
random_18.txt AC 249 ms 270172 KiB
random_19.txt AC 259 ms 269984 KiB
random_20.txt AC 243 ms 269864 KiB
random_21.txt AC 298 ms 271160 KiB
random_22.txt AC 244 ms 269668 KiB
random_23.txt AC 260 ms 270040 KiB
random_24.txt AC 344 ms 307224 KiB
random_25.txt AC 278 ms 270600 KiB
random_26.txt AC 260 ms 270184 KiB
random_27.txt AC 1038 ms 338772 KiB
random_28.txt AC 249 ms 269372 KiB
random_29.txt AC 296 ms 271012 KiB
random_30.txt AC 329 ms 299564 KiB
random_31.txt AC 505 ms 317884 KiB
random_32.txt AC 321 ms 294496 KiB