中級 2件解決済み 正答率100.0%

Robot Command Simulator

Move a robot using L (turn left 90 degrees), R (turn right 90 degrees), and A (advance one step).

このチャレンジで学べること

  • Track position and facing direction
  • Turn left and right with direction arrays
  • Process a command string step by step

問題

翻訳を表示

Move a robot using L (turn left 90 degrees), R (turn right 90 degrees), and A (advance one step). Turning does not move it. Positive x points east and positive y points north. Directions are encoded as 0 = north, 1 = east, 2 = south, 3 = west. There are no obstacles or boundaries.

Implement robot_command_simulator(x, y, direction, commands).

Input

x: Initial horizontal coordinate.

y: Initial vertical coordinate.

direction: Initial direction: 0 north, 1 east, 2 south, 3 west.

commands: Commands to process from left to right.

Output

Return [final_x, final_y, final_direction], using the same direction encoding.

制約

−1,000,000,000 ≤ initial x, y ≤ 1,000,000,000. Direction is 0…3. There are 0…100,000 commands, each uppercase L, R, or A.

例

例 1
入力:
x = 0
y = 0
direction = 0
commands = "RAALA"

期待される出力:
expected = [2, 1, 0]
入力:
long long x = 0;
long long y = 0;
long long direction = 0;
std::string commands = "RAALA";

期待される出力:
std::vector<long long> expected = {2, 1, 0};
入力:
long x = 0;
long y = 0;
long direction = 0;
string commands = "RAALA";

期待される出力:
long[] expected = new long[] {2, 1, 0};

解説

Turn east, advance twice to (2, 0), turn north, then advance to (2, 1). Direction 0 means north.

例 2
入力:
x = 3
y = -2
direction = 1
commands = "LLLL"

期待される出力:
expected = [3, -2, 1]
入力:
long long x = 3;
long long y = -2;
long long direction = 1;
std::string commands = "LLLL";

期待される出力:
std::vector<long long> expected = {3, -2, 1};
入力:
long x = 3;
long y = -2;
long direction = 1;
string commands = "LLLL";

期待される出力:
long[] expected = new long[] {3, -2, 1};

解説

Four left turns restore the original east-facing direction (1). Turning never changes the coordinates.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Track position and direction separately; turns change orientation without moving the robot.

ヒント 2

Advance uses the current direction to update one coordinate by one step.

ヒント 3

Remember the direction wraps around after turning left from north or right from west.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 859 ms - 5/5 コードを見る
2 RCRiccardo Corti Python 872 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が2件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。