結果

問題 No.232 めぐるはめぐる (2)
ユーザー kichirb3kichirb3
提出日時 2018-04-02 15:48:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 142 ms / 1,000 ms
コード長 3,250 bytes
コンパイル時間 71 ms
コンパイル使用メモリ 11,140 KB
実行使用メモリ 17,712 KB
最終ジャッジ日時 2023-10-12 13:48:38
合計ジャッジ時間 2,428 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
11,520 KB
testcase_01 AC 142 ms
17,712 KB
testcase_02 AC 115 ms
11,448 KB
testcase_03 AC 120 ms
11,556 KB
testcase_04 AC 16 ms
8,180 KB
testcase_05 AC 16 ms
8,228 KB
testcase_06 AC 16 ms
8,048 KB
testcase_07 AC 16 ms
8,056 KB
testcase_08 AC 101 ms
13,988 KB
testcase_09 AC 16 ms
8,056 KB
testcase_10 AC 17 ms
8,044 KB
testcase_11 AC 16 ms
8,088 KB
testcase_12 AC 16 ms
8,184 KB
testcase_13 AC 16 ms
8,100 KB
testcase_14 AC 16 ms
8,096 KB
testcase_15 AC 16 ms
8,124 KB
testcase_16 AC 16 ms
8,096 KB
testcase_17 AC 16 ms
8,060 KB
testcase_18 AC 17 ms
8,456 KB
testcase_19 AC 16 ms
8,024 KB
testcase_20 AC 16 ms
8,024 KB
testcase_21 AC 17 ms
8,012 KB
testcase_22 AC 16 ms
8,100 KB
testcase_23 AC 16 ms
8,048 KB
testcase_24 AC 16 ms
8,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
No.232 めぐるはめぐる (2)
https://yukicoder.me/problems/no/232

"""
import sys
from sys import stdin
input = stdin.readline

cx, cy = 0, 0                          # 現在の座標

def last_step(steps, gx, gy):
    # 次の移動でゴール地点に飛び込むための移動コマンドを入力
    nx, ny = '', ''
    if cx < gx:
        nx = '>'
    elif cx > gx:
        nx = '<'
    if cy < gy:
        ny = '^'
    elif cy > gy:
        ny = 'v'
    steps.append('{}{}'.format(nx, ny))


def guruguru(steps, gx, gy):
    # ゴール地点の周辺を時計周りにグルグルする
    # (「各行は 1 文字ないし 2 文字の文字列を出力すればいい」ので、移動しない('')という選択は無し)
    global cx, cy
    dx = gx - cx
    dy = gy - cy
    if dx == 1 and dy == 1:
        steps.append('^')
        cy += 1
    elif dx == 1 and dy == 0:
        steps.append('^')
        cy += 1
    elif dx == 1 and dy == -1:
        steps.append('>')
        cx += 1
    elif dx == 0 and dy == -1:
        steps.append('>')
        cx += 1
    elif dx == -1 and dy == -1:
        steps.append('v')
        cy -= 1
    elif dx == -1 and dy == 0:
        steps.append('v')
        cy -= 1
    elif dx == -1 and dy == 1:
        steps.append('<')
        cx -= 1
    else:
        steps.append('<')
        cx -= 1

def one_step_closer(steps, gx, gy):
    # 最短ルートでゴール地点に近づく(ただし、ゴール地点には飛び込まない)
    global cx, cy
    nx, ny = '', ''
    dx = gx - cx
    dy = gy - cy
    if max(abs(dx), abs(dy)) <= 1:
        guruguru(steps, gx, gy)
    else:
        if dx > 0:
            nx = '>'
            cx += 1
        elif dx < 0:
            nx = '<'
            cx -= 1
        if dy > 0:
            ny = '^'
            cy += 1
        elif dy < 0:
            ny = 'v'
            cy -= 1
        steps.append('{}{}'.format(nx, ny))


def solve(T, A, B):
    ans = []                    # コントローラから入力する移動キー
    global cx, cy              # 現在の座標
    gx, gy = B, A               # ゴール地点の座長

    # 特殊ケース
    if gx > T or gy > T:       # ななめに移動しても間に合わないケース
        return ans
    if cx == gx and cy == gy:  #  スタート地点とゴール地点が同じ場合
        if T == 1:
            return ans         # 最初からゴール地点にいるので、1ターン目でゴールに「到着」することはできない
        else:
            ans.append('{}{}'.format('>', ''))
            cx += 1
            T -= 1

    # 通常ケース
    while T:
        if T == 1:              # 最後の1ターンでゴール地点に飛び込む
            last_step(ans, gx, gy)
            return ans
        else:
            one_step_closer(ans, gx, gy)  # 最短距離でゴールに近づく。ターンに余裕があればゴール周辺でグルグルする
            T -= 1


def main(args):
    T, A, B = map(int, input().split())
    ans = solve(T, A, B)
    if not ans:
        print('NO')
    else:
        print('YES')
        print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])
0