結果
| 問題 |
No.232 めぐるはめぐる (2)
|
| コンテスト | |
| ユーザー |
kichirb3
|
| 提出日時 | 2018-04-02 15:48:04 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 183 ms / 1,000 ms |
| コード長 | 3,250 bytes |
| コンパイル時間 | 498 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 18,944 KB |
| 最終ジャッジ日時 | 2024-09-14 12:44:48 |
| 合計ジャッジ時間 | 2,752 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
# -*- 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:])
kichirb3