문제
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.
예시
입력:
x = 0
y = 0
commands = "RAALA"
direction = 0
예상 출력:
expected = [2, 1, 0]
입력:
long long x = 0;
long long y = 0;
std::string commands = "RAALA";
long long direction = 0;
예상 출력:
std::vector<long long> expected = {2, 1, 0};
입력:
long x = 0;
long y = 0;
string commands = "RAALA";
long direction = 0;
예상 출력:
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.
입력:
x = 3
y = -2
commands = "LLLL"
direction = 1
예상 출력:
expected = [3, -2, 1]
입력:
long long x = 3;
long long y = -2;
std::string commands = "LLLL";
long long direction = 1;
예상 출력:
std::vector<long long> expected = {3, -2, 1};
입력:
long x = 3;
long y = -2;
string commands = "LLLL";
long direction = 1;
예상 출력:
long[] expected = new long[] {3, -2, 1};
설명
Four left turns restore the original east-facing direction (1). Turning never changes the coordinates.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
위치와 방향을 따로 추적하세요. 회전은 로봇을 움직이지 않고 방향만 바꿉니다.
힌트 2
advance는 현재 방향을 사용해 좌표 하나를 한 칸 갱신합니다.
힌트 3
north에서 left, west에서 right를 돌면 방향이 순환된다는 점을 기억하세요.
예상 복잡도
시간 O(n), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.