समस्या
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
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.
इनपुट:
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
Position और direction को अलग-अलग track करो; turns robot को घुमाते हैं, move नहीं कराते।
संकेत 2
Advance current direction के हिसाब से एक coordinate को एक step update करता है।
संकेत 3
याद रखो, north से left turn या west से right turn के बाद direction wrap हो जाती है।
अपेक्षित जटिलता
समय O(n), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।