forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot Room Cleaner.java
More file actions
63 lines (58 loc) · 1.78 KB
/
Robot Room Cleaner.java
File metadata and controls
63 lines (58 loc) · 1.78 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
/**
* // This is the robot's control interface.
* // You should not implement it, or speculate about its implementation
* interface Robot {
* // Returns true if the cell in front is open and robot moves into the cell.
* // Returns false if the cell in front is blocked and robot stays in the current cell.
* public boolean move();
*
* // Robot will stay in the same cell after calling turnLeft/turnRight.
* // Each turn will be 90 degrees.
* public void turnLeft();
* public void turnRight();
*
* // Clean the current cell.
* public void clean();
* }
*/
class Solution {
public void cleanRoom(Robot robot) {
Set<String> visited = new HashSet<>();
backtrack(robot, visited, 0, 0, 0);
}
private void backtrack(Robot robot, Set<String> visited, int x, int y, int dir) {
String key = x + "|" + y;
if (visited.contains(key)) {
return;
}
visited.add(key);
robot.clean();
for (int k = 0; k < 4; k++) {
int i = x;
int j = y;
if (robot.move()) {
if (dir == 0) {
i = x - 1;
}
else if (dir == 1) {
j = y + 1;
}
else if (dir == 2) {
i = x + 1;
}
else {
j = y - 1;
}
backtrack(robot, visited, i, j, dir);
robot.turnLeft();
robot.turnLeft();
robot.move();
robot.turnRight();
robot.turnRight();
}
robot.turnRight();
dir += 1;
dir %= 4;
}
}
}