Skip to content

Commit ac3bed4

Browse files
committed
feat: add solutions to lc problem: No.0489
No.0489.Robot Room Cleaner
1 parent 63fb8e5 commit ac3bed4

File tree

6 files changed

+649
-1
lines changed

6 files changed

+649
-1
lines changed

solution/0400-0499/0489.Robot Room Cleaner/README.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,248 @@ col = 3
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
DFS。
65+
66+
我们设定机器人起始位置 `(0, 0)`,朝向 d = 0。
67+
68+
将起始位置进行清扫,并进行标记(即清扫过的格子也算作障碍);然后依次选择四个朝向 up,right,down 和 left 进行深度优先搜索,相邻的两个朝向仅差一次向右旋转的操作;
69+
70+
- 对于选择的朝向,检查下一个格子是否有障碍,如果没有,则向对应朝向移动一格,并开始新的搜索;
71+
- 如果有,则向右旋转。
72+
73+
如果四个朝向都搜索完毕,则回溯到上一次搜索。
74+
6475
<!-- tabs:start -->
6576

6677
### **Python3**
6778

6879
<!-- 这里可写当前语言的特殊实现逻辑 -->
6980

7081
```python
82+
# """
83+
# This is the robot's control interface.
84+
# You should not implement it, or speculate about its implementation
85+
# """
86+
# class Robot:
87+
# def move(self):
88+
# """
89+
# Returns true if the cell in front is open and robot moves into the cell.
90+
# Returns false if the cell in front is blocked and robot stays in the current cell.
91+
# :rtype bool
92+
# """
93+
#
94+
# def turnLeft(self):
95+
# """
96+
# Robot will stay in the same cell after calling turnLeft/turnRight.
97+
# Each turn will be 90 degrees.
98+
# :rtype void
99+
# """
100+
#
101+
# def turnRight(self):
102+
# """
103+
# Robot will stay in the same cell after calling turnLeft/turnRight.
104+
# Each turn will be 90 degrees.
105+
# :rtype void
106+
# """
107+
#
108+
# def clean(self):
109+
# """
110+
# Clean the current cell.
111+
# :rtype void
112+
# """
113+
114+
class Solution:
115+
def cleanRoom(self, robot):
116+
"""
117+
:type robot: Robot
118+
:rtype: None
119+
"""
120+
def back():
121+
robot.turnRight()
122+
robot.turnRight()
123+
robot.move()
124+
robot.turnRight()
125+
robot.turnRight()
71126

127+
def dfs(i, j, d):
128+
vis.add((i, j))
129+
robot.clean()
130+
for k in range(4):
131+
nd = (d + k) % 4
132+
x, y = i + dirs[nd][0], j + dirs[nd][1]
133+
if (x, y) not in vis and robot.move():
134+
dfs(x, y, nd)
135+
back()
136+
robot.turnRight()
137+
138+
vis = set()
139+
dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
140+
dfs(0, 0, 0)
72141
```
73142

74143
### **Java**
75144

76145
<!-- 这里可写当前语言的特殊实现逻辑 -->
77146

78147
```java
148+
/**
149+
* // This is the robot's control interface.
150+
* // You should not implement it, or speculate about its implementation
151+
* interface Robot {
152+
* // Returns true if the cell in front is open and robot moves into the cell.
153+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
154+
* public boolean move();
155+
*
156+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
157+
* // Each turn will be 90 degrees.
158+
* public void turnLeft();
159+
* public void turnRight();
160+
*
161+
* // Clean the current cell.
162+
* public void clean();
163+
* }
164+
*/
165+
166+
class Solution {
167+
private Set<String> vis;
168+
private int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
169+
170+
public void cleanRoom(Robot robot) {
171+
vis = new HashSet<>();
172+
dfs(0, 0, 0, robot);
173+
}
79174

175+
private void dfs(int i, int j, int d, Robot robot) {
176+
vis.add(i + "," + j);
177+
robot.clean();
178+
for (int k = 0; k < 4; ++k) {
179+
int nd = (d + k) % 4;
180+
int x = i + dirs[nd][0];
181+
int y = j + dirs[nd][1];
182+
if (!vis.contains(x + "," + y) && robot.move()) {
183+
dfs(x, y, nd, robot);
184+
back(robot);
185+
}
186+
robot.turnRight();
187+
}
188+
}
189+
190+
private void back(Robot robot) {
191+
robot.turnRight();
192+
robot.turnRight();
193+
robot.move();
194+
robot.turnRight();
195+
robot.turnRight();
196+
}
197+
}
198+
```
199+
200+
### **C++**
201+
202+
```cpp
203+
/**
204+
* // This is the robot's control interface.
205+
* // You should not implement it, or speculate about its implementation
206+
* class Robot {
207+
* public:
208+
* // Returns true if the cell in front is open and robot moves into the cell.
209+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
210+
* bool move();
211+
*
212+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
213+
* // Each turn will be 90 degrees.
214+
* void turnLeft();
215+
* void turnRight();
216+
*
217+
* // Clean the current cell.
218+
* void clean();
219+
* };
220+
*/
221+
222+
class Solution {
223+
public:
224+
vector<vector<int>> dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
225+
226+
void cleanRoom(Robot& robot) {
227+
unordered_set<string> vis;
228+
dfs(0, 0, 0, vis, robot);
229+
}
230+
231+
void dfs(int i, int j, int d, unordered_set<string>& vis, Robot& robot) {
232+
vis.insert(to_string(i) + "," + to_string(j));
233+
robot.clean();
234+
for (int k = 0; k < 4; ++k)
235+
{
236+
int nd = (d + k) % 4;
237+
int x = i + dirs[nd][0];
238+
int y = j + dirs[nd][1];
239+
if (!vis.count(to_string(x) + "," + to_string(y)) && robot.move())
240+
{
241+
dfs(x, y, nd, vis, robot);
242+
back(robot);
243+
}
244+
robot.turnRight();
245+
}
246+
}
247+
248+
void back(Robot& robot) {
249+
robot.turnRight();
250+
robot.turnRight();
251+
robot.move();
252+
robot.turnRight();
253+
robot.turnRight();
254+
}
255+
};
256+
```
257+
258+
### **Go**
259+
260+
```go
261+
/**
262+
* // This is the robot's control interface.
263+
* // You should not implement it, or speculate about its implementation
264+
* type Robot struct {
265+
* }
266+
*
267+
* // Returns true if the cell in front is open and robot moves into the cell.
268+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
269+
* func (robot *Robot) Move() bool {}
270+
*
271+
* // Robot will stay in the same cell after calling TurnLeft/TurnRight.
272+
* // Each turn will be 90 degrees.
273+
* func (robot *Robot) TurnLeft() {}
274+
* func (robot *Robot) TurnRight() {}
275+
*
276+
* // Clean the current cell.
277+
* func (robot *Robot) Clean() {}
278+
*/
279+
280+
func cleanRoom(robot *Robot) {
281+
vis := make(map[string]bool)
282+
dirs := [][]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
283+
back := func() {
284+
robot.TurnRight()
285+
robot.TurnRight()
286+
robot.Move()
287+
robot.TurnRight()
288+
robot.TurnRight()
289+
}
290+
var dfs func(i, j, d int)
291+
dfs = func(i, j, d int) {
292+
vis[strconv.Itoa(i)+","+strconv.Itoa(j)] = true
293+
robot.Clean()
294+
for k := 0; k < 4; k++ {
295+
nd := (d + k) % 4
296+
x, y := i+dirs[nd][0], j+dirs[nd][1]
297+
if !vis[strconv.Itoa(x)+","+strconv.Itoa(y)] && robot.Move() {
298+
dfs(x, y, nd)
299+
back()
300+
}
301+
robot.TurnRight()
302+
}
303+
}
304+
dfs(0, 0, 0)
305+
}
80306
```
81307

82308
### **...**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy