Problem
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.
Einschränkungen
−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.
Beispiele
Eingabe:
x = 0
y = 0
commands = "RAALA"
direction = 0
Erwartete Ausgabe:
expected = [2, 1, 0]
Eingabe:
long long x = 0;
long long y = 0;
std::string commands = "RAALA";
long long direction = 0;
Erwartete Ausgabe:
std::vector<long long> expected = {2, 1, 0};
Eingabe:
long x = 0;
long y = 0;
string commands = "RAALA";
long direction = 0;
Erwartete Ausgabe:
long[] expected = new long[] {2, 1, 0};
Erläuterung
Turn east, advance twice to (2, 0), turn north, then advance to (2, 1). Direction 0 means north.
Eingabe:
x = 3
y = -2
commands = "LLLL"
direction = 1
Erwartete Ausgabe:
expected = [3, -2, 1]
Eingabe:
long long x = 3;
long long y = -2;
std::string commands = "LLLL";
long long direction = 1;
Erwartete Ausgabe:
std::vector<long long> expected = {3, -2, 1};
Eingabe:
long x = 3;
long y = -2;
string commands = "LLLL";
long direction = 1;
Erwartete Ausgabe:
long[] expected = new long[] {3, -2, 1};
Erläuterung
Four left turns restore the original east-facing direction (1). Turning never changes the coordinates.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Verfolge Position und Richtung getrennt; Drehungen ändern nur die Ausrichtung, nicht die Position des Roboters.
Hinweis 2
Advance nutzt die aktuelle Richtung, um eine Koordinate um einen Schritt zu ändern.
Hinweis 3
Denk daran, dass die Richtung nach links von Norden oder nach rechts von Westen wieder umbricht.
Erwartete Komplexität
Zeit O(n), zusätzlicher Speicher O(1)
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.