모든 챌린지
보통 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

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 ННикита Python 859 ms - 5/5 코드 보기
2 RCRiccardo Corti Python 872 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 2개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.