Skip to content

Commit 79519a0

Browse files
authored
Added task 2069.
1 parent 69d7fa5 commit 79519a0

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g2001_2100.s2069_walking_robot_simulation_ii;
2+
3+
// #Medium #Design #Simulation #2022_05_30_Time_110_ms_(56.14%)_Space_95.2_MB_(53.51%)
4+
5+
public class Robot {
6+
private int p;
7+
private int w;
8+
private int h;
9+
10+
public Robot(int width, int height) {
11+
w = width - 1;
12+
h = height - 1;
13+
p = 0;
14+
}
15+
16+
public void step(int num) {
17+
p += num;
18+
}
19+
20+
public int[] getPos() {
21+
int remain = p % (2 * (w + h));
22+
if (remain <= w) {
23+
return new int[] {remain, 0};
24+
}
25+
remain -= w;
26+
if (remain <= h) {
27+
return new int[] {w, remain};
28+
}
29+
remain -= h;
30+
if (remain <= w) {
31+
return new int[] {w - remain, h};
32+
}
33+
remain -= w;
34+
return new int[] {0, h - remain};
35+
}
36+
37+
public String getDir() {
38+
int[] pos = getPos();
39+
if (p == 0 || pos[1] == 0 && pos[0] > 0) {
40+
return "East";
41+
} else if (pos[0] == w && pos[1] > 0) {
42+
return "North";
43+
} else if (pos[1] == h && pos[0] < w) {
44+
return "West";
45+
} else {
46+
return "South";
47+
}
48+
}
49+
}
50+
51+
/*
52+
* Your Robot object will be instantiated and called as such:
53+
* Robot obj = new Robot(width, height);
54+
* obj.step(num);
55+
* int[] param_2 = obj.getPos();
56+
* String param_3 = obj.getDir();
57+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2069\. Walking Robot Simulation II
2+
3+
Medium
4+
5+
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"`.
6+
7+
The robot can be instructed to move for a specific number of **steps**. For each step, it does the following.
8+
9+
1. Attempts to move **forward one** cell in the direction it is facing.
10+
2. If the cell the robot is **moving to** is **out of bounds**, the robot instead **turns** 90 degrees **counterclockwise** and retries the step.
11+
12+
After the robot finishes moving the number of steps required, it stops and awaits the next instruction.
13+
14+
Implement the `Robot` class:
15+
16+
* `Robot(int width, int height)` Initializes the `width x height` grid with the robot at `(0, 0)` facing `"East"`.
17+
* `void step(int num)` Instructs the robot to move forward `num` steps.
18+
* `int[] getPos()` Returns the current cell the robot is at, as an array of length 2, `[x, y]`.
19+
* `String getDir()` Returns the current direction of the robot, `"North"`, `"East"`, `"South"`, or `"West"`.
20+
21+
**Example 1:**
22+
23+
![example-1](https://assets.leetcode.com/uploads/2021/10/09/example-1.png)
24+
25+
**Input**
26+
27+
["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
28+
29+
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
30+
31+
**Output:**
32+
33+
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
34+
35+
**Explanation:**
36+
37+
Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
38+
robot.step(2); // It moves two steps East to (2, 0), and faces East.
39+
robot.step(2); // It moves two steps East to (4, 0), and faces East.
40+
robot.getPos(); // return [4, 0]
41+
robot.getDir(); // return "East"
42+
robot.step(2); // It moves one step East to (5, 0), and faces East.
43+
// Moving the next step East would be out of bounds, so it turns and faces North.
44+
// Then, it moves one step North to (5, 1), and faces North.
45+
robot.step(1); // It moves one step North to (5, 2), and faces North (not West).
46+
robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West.
47+
// Then, it moves four steps West to (1, 2), and faces West.
48+
robot.getPos(); // return [1, 2]
49+
robot.getDir(); // return "West"
50+
51+
**Constraints:**
52+
53+
* `2 <= width, height <= 100`
54+
* <code>1 <= num <= 10<sup>5</sup></code>
55+
* At most <code>10<sup>4</sup></code> calls **in total** will be made to `step`, `getPos`, and `getDir`.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g2001_2100.s2069_walking_robot_simulation_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class RobotTest {
9+
@Test
10+
void robot() {
11+
// Initialize the grid and the robot at (0, 0) facing East.
12+
Robot robot = new Robot(6, 3);
13+
// It moves two steps East to (2, 0), and faces East.
14+
robot.step(2);
15+
// It moves two steps East to (4, 0), and faces East.
16+
robot.step(2);
17+
// return [4, 0]
18+
assertThat(robot.getPos(), equalTo(new int[] {4, 0}));
19+
// return "East"
20+
robot.getDir();
21+
// It moves one step East to (5, 0), and faces East.
22+
// Moving the next step East would be out of bounds, so it turns and faces North.
23+
// Then, it moves one step North to (5, 1), and faces North.
24+
robot.step(2);
25+
// It moves one step North to (5, 2), and faces North (not West).
26+
robot.step(1);
27+
// Moving the next step North would be out of bounds, so it turns and faces West.
28+
// Then, it moves four steps West to (1, 2), and faces West.
29+
robot.step(4);
30+
// return [1, 2]
31+
assertThat(robot.getPos(), equalTo(new int[] {1, 2}));
32+
// return "West"
33+
assertThat(robot.getDir(), equalTo("West"));
34+
}
35+
36+
@Test
37+
void robot2() {
38+
// Initialize the grid and the robot at (0, 0) facing East.
39+
Robot robot = new Robot(0, 0);
40+
// It moves two steps East to (0, -2), and faces East.
41+
robot.step(2);
42+
// It moves two steps East to (0, -4), and faces East.
43+
robot.step(2);
44+
// return [0, -4]
45+
assertThat(robot.getPos(), equalTo(new int[] {0, -4}));
46+
}
47+
48+
@Test
49+
void robot3() {
50+
// Initialize the grid and the robot at (0, 0) facing East.
51+
Robot robot = new Robot(2, 2);
52+
// It moves two steps East to (2, 0), and faces East.
53+
robot.step(2);
54+
// It moves two steps East to (4, 0), and faces East.
55+
robot.step(2);
56+
// It moves two steps East to (6, 0), and faces East.
57+
robot.step(2);
58+
// return [1, 1]
59+
assertThat(robot.getPos(), equalTo(new int[] {1, 1}));
60+
}
61+
}

0 commit comments

Comments
 (0)