Tất cả thử thách
Trung bình 2 người đã giải Tỷ lệ chấp nhận 100.0%

Robot Command Simulator

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

−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.

Ví dụ

Ví dụ 1
Đầu vào:
x = 0
y = 0
direction = 0
commands = "RAALA"

Kết quả mong đợi:
expected = [2, 1, 0]
Đầu vào:
long long x = 0;
long long y = 0;
long long direction = 0;
std::string commands = "RAALA";

Kết quả mong đợi:
std::vector<long long> expected = {2, 1, 0};
Đầu vào:
long x = 0;
long y = 0;
long direction = 0;
string commands = "RAALA";

Kết quả mong đợi:
long[] expected = new long[] {2, 1, 0};

Giải thích

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

Ví dụ 2
Đầu vào:
x = 3
y = -2
direction = 1
commands = "LLLL"

Kết quả mong đợi:
expected = [3, -2, 1]
Đầu vào:
long long x = 3;
long long y = -2;
long long direction = 1;
std::string commands = "LLLL";

Kết quả mong đợi:
std::vector<long long> expected = {3, -2, 1};
Đầu vào:
long x = 3;
long y = -2;
long direction = 1;
string commands = "LLLL";

Kết quả mong đợi:
long[] expected = new long[] {3, -2, 1};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 859 ms - 5/5 Xem code
2 RCRiccardo Corti Python 872 ms - 4/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 2 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.