-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (104 loc) · 4.3 KB
/
main.cpp
File metadata and controls
112 lines (104 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Source: https://leetcode.com/problems/walking-robot-simulation-ii
// Title: Walking Robot Simulation II
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// A `width x height` grid is on an XY-plane with the **bottom-left** cell at `(0, 0)` and the **top-right** cell at `(width - 1, height - 1)`. The grid is aligned with the four cardinal directions (`"North"`, `"East"`, `"South"`, and `"West"`). A robot is **initially** at cell `(0, 0)` facing direction `"East"`.
//
// The robot can be instructed to move for a specific number of **steps**. For each step, it does the following.
//
// - Attempts to move **forward one** cell in the direction it is facing.
// - If the cell the robot is **moving to** is **out of bounds**, the robot instead **turns** 90 degrees **counterclockwise** and retries the step.
//
// After the robot finishes moving the number of steps required, it stops and awaits the next instruction.
//
// Implement the `Robot` class:
//
// - `Robot(int width, int height)` Initializes the `width x height` grid with the robot at `(0, 0)` facing `"East"`.
// - `void step(int num)` Instructs the robot to move forward `num` steps.
// - `int[] getPos()` Returns the current cell the robot is at, as an array of length 2, `[x, y]`.
// - `String getDir()` Returns the current direction of the robot, `"North"`, `"East"`, `"South"`, or `"West"`.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2021/10/09/example-1.png
//
// ```
// Input:
// ["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
// [[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
// Output:
// [null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
// Explanation:
// Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
// robot.step(2); // It moves two steps East to (2, 0), and faces East.
// robot.step(2); // It moves two steps East to (4, 0), and faces East.
// robot.getPos(); // return [4, 0]
// robot.getDir(); // return "East"
// robot.step(2); // It moves one step East to (5, 0), and faces East.
// // Moving the next step East would be out of bounds, so it turns and faces North.
// // Then, it moves one step North to (5, 1), and faces North.
// robot.step(1); // It moves one step North to (5, 2), and faces **North** (not West).
// robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West.
// // Then, it moves four steps West to (1, 2), and faces West.
// robot.getPos(); // return [1, 2]
// robot.getDir(); // return "West"
// ```
//
// **Constraints:**
//
// - `2 <= width, height <= 100`
// - `1 <= num <= 10^5`
// - At most `10^4` calls **in total** will be made to `step`, `getPos`, and `getDir`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
using namespace std;
// The bot will always be at the border of the gird.
// We only need to compute the position.
//
// We define the stating position as -1,
// since the facing is different when we finished a loop.
class Robot {
const int maxX;
const int maxY;
const int bottomEnd; // end of bottom border position
const int rightEnd; // end of right border position
const int topEnd; // end of top border position
const int leftEnd; // end of left border position
int pos;
public:
Robot(int width, int height)
: maxX(width - 1),
maxY(height - 1),
bottomEnd(maxX),
rightEnd(maxX + maxY),
topEnd(2 * maxX + maxY),
leftEnd(2 * maxX + 2 * maxY),
pos(-1) {}
void step(int num) {
pos += num;
pos %= leftEnd;
}
vector<int> getPos() {
if (pos < bottomEnd) {
return {pos + 1, 0};
} else if (pos < rightEnd) {
return {maxX, pos - bottomEnd + 1};
} else if (pos < topEnd) {
return {topEnd - pos - 1, maxY};
} else {
return {0, leftEnd - pos - 1};
}
}
string getDir() {
if (pos < bottomEnd) {
return "East";
} else if (pos < rightEnd) {
return "North";
} else if (pos < topEnd) {
return "West";
} else {
return "South";
}
}
};