-
Notifications
You must be signed in to change notification settings - Fork 92
/
RobotRoomCleaner.java
218 lines (195 loc) · 6.39 KB
/
RobotRoomCleaner.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Given a robot cleaner in a room modeled as a grid.
*
* Each cell in the grid can be empty or blocked.
*
* The robot cleaner with 4 given APIs can move forward, turn left or turn
* right. Each turn it made is 90 degrees.
*
* When it tries to move into a blocked cell, its bumper sensor detects the
* obstacle and it stays on the current cell.
*
* Design an algorithm to clean the entire room using only the 4 given APIs
* shown below.
*
* interface Robot {
* // returns true if next cell is open and robot moves into the cell.
* // returns false if next cell is obstacle and robot stays on the current cell.
* boolean move();
*
* // Robot will stay on the same cell after calling turnLeft/turnRight.
* // Each turn will be 90 degrees.
* void turnLeft();
* void turnRight();
*
* // Clean the current cell.
* void clean();
* }
*
* Example:
* Input:
* room = [
* [1,1,1,1,1,0,1,1],
* [1,1,1,1,1,0,1,1],
* [1,0,1,1,1,1,1,1],
* [0,0,0,1,0,0,0,0],
* [1,1,1,1,1,1,1,1]
* ],
* row = 1,
* col = 3
*
* Explanation:
* All grids in the room are marked by either 0 or 1.
* 0 means the cell is blocked, while 1 means the cell is accessible.
* The robot initially starts at the position of row=1, col=3.
* From the top left corner, its position is one row below and three columns right.
*
* Notes:
* - The input is only given to initialize the room and the robot's position
* internally. You must solve this problem "blindfolded". In other words,
* you must control the robot using only the mentioned 4 APIs, without knowing
* the room layout and the initial robot's position.
* - The robot's initial position will always be in an accessible cell.
* - The initial direction of the robot will be facing up.
* - All accessible cells are connected, which means the all cells marked as 1
* will be accessible by the robot.
* - Assume all four edges of the grid are all surrounded by wall.
*/
/**
* // 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();
* }
*/
public class RobotRoomCleaner {
public void cleanRoom(Robot robot) {
cleanRoom(robot, new HashMap<Integer, Set<Integer>>(), new int[2], 0, true);
}
public void cleanRoom(Robot robot, Map<Integer, Set<Integer>> map, int[] pos, int dir, boolean in) {
robot.clean();
if (!map.containsKey(pos[0])) {
map.put(pos[0], new HashSet<>());
}
map.get(pos[0]).add(pos[1]);
int[] nxt = next(pos, dir);
if (!contains(map, nxt) && robot.move()) {
cleanRoom(robot, map, nxt, dir, false);
}
dir = (dir + 1) % 4;
robot.turnLeft();
nxt = next(pos, dir);
if (!contains(map, nxt) && robot.move()) {
cleanRoom(robot, map, nxt, dir, false);
}
dir = (dir + 1) % 4;
robot.turnLeft();
nxt = next(pos, dir);
if (in && !contains(map, nxt) && robot.move()) {
cleanRoom(robot, map, nxt, dir, false);
}
dir = (dir + 1) % 4;
robot.turnLeft();
nxt = next(pos, dir);
if (!contains(map, nxt) && robot.move()) {
cleanRoom(robot, map, nxt, dir, false);
}
robot.turnRight();
robot.move();
robot.turnLeft();
robot.turnLeft();
}
private boolean contains(Map<Integer, Set<Integer>> map, int[] pos) {
return map.containsKey(pos[0]) && map.get(pos[0]).contains(pos[1]);
}
private int[] next(int[] now, int dir) {
if (dir == 0) {
return up(now);
} else if (dir == 1) {
return left(now);
} else if (dir == 2) {
return down(now);
} else {
return right(now);
}
}
private int[] up(int[] now) {
return new int[]{now[0]-1, now[1]};
}
private int[] left(int[] now) {
return new int[]{now[0], now[1]-1};
}
private int[] down(int[] now) {
return new int[]{now[0]+1, now[1]};
}
private int[] right(int[] now) {
return new int[]{now[0], now[1]+1};
}
public void cleanRoom2(Robot robot) {
dfs(robot, new HashMap<>(), new int[2], 0);
}
private void dfs(Robot robot, Map<Integer, Set<Integer>> visited, int[] pos, int dir) {
// System.out.println(pos[0] + ", " + pos[1] + ": " + dir);
if (visited.containsKey(pos[0]) && visited.get(pos[0]).contains(pos[1])) {
robot.turnRight();
robot.turnRight();
robot.move();
return;
}
if (!visited.containsKey(pos[0])) visited.put(pos[0], new HashSet<>());
visited.get(pos[0]).add(pos[1]);
robot.clean();
if (robot.move()) {
dfs(robot, visited, nextPos(pos, dir), dir);
} else {
robot.turnRight();
robot.turnRight();
}
robot.turnRight();
dir = nextDir(dir, 3);
if (robot.move()) {
dfs(robot, visited, nextPos(pos, dir), dir);
} else {
robot.turnRight();
robot.turnRight();
}
dir = nextDir(dir, 2);
if (robot.move()) {
dfs(robot, visited, nextPos(pos, dir), dir);
robot.turnLeft();
} else {
robot.turnRight();
}
dir = nextDir(dir, 1);
if (robot.move()) {
dfs(robot, visited, nextPos(pos, dir), dir);
robot.turnRight();
robot.turnRight();
}
robot.move();
}
private int nextDir(int dir, int move) {
return (dir + move) % 4;
}
private int[] nextPos(int[] pos, int dir) {
if (dir == 0) {
return new int[]{pos[0], pos[1]+1};
} else if (dir == 1) {
return new int[]{pos[0]+1, pos[1]};
} else if (dir == 2) {
return new int[]{pos[0], pos[1]-1};
} else {
return new int[]{pos[0]-1, pos[1]};
}
}
}