Skip to content

Commit 9b5eaf8

Browse files
author
Partho Biswas
committed
89. Robot Room Cleaner
1 parent 65aee07 commit 9b5eaf8

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
476476
|24| **[1197. Minimum Knight Moves](https://tinyurl.com/svox8jw)** | [Python](https://tinyurl.com/wu6rdaw/1197_Minimum_Knight_Moves.py)| **[Art 1](https://tinyurl.com/wed5c6s)** | Medium | TODO: Check AGain. Important. |
477477
|25| **[1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/)** | [Python](https://tinyurl.com/wu6rdaw/1066_Campus_Bikes_II.py)| [Vid 1](https://tinyurl.com/qq6obzv), **[Art 1](https://tinyurl.com/r4wtfbo)** | Medium |📌 **TODO: CHECK Dijkstra approach again. Backtracking solution is getting TLE. Solve it and implement it with DP also. Very important** |
478478
|26| **[752. Open the Lock](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/752_Open_the_Lock.py)| **[Art 1](https://tinyurl.com/s3oskv8)**, **[Art 2](https://tinyurl.com/rsr4tz9)** | Medium |📌 **TODO: CHECK again. Very important and interesting problem. Loved it** |
479+
|27| **[489. Robot Room Cleaner](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/489_Robot_Room_Cleaner.py), [Swift](https://tinyurl.com/wuja3c4/489_Robot_Room_Cleaner.swift) | **[Vid 1](https://tinyurl.com/vahalvm)**, **[Vid 2](https://tinyurl.com/qpumkro)**, **[Art 1](https://tinyurl.com/rnu8679)** | Hard |📌 **TODO: CHECK again. Very important and interesting problem. Loved it** |
479480

480481

481482
</p>
@@ -522,7 +523,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
522523
|16| [39. Combination Sum](https://leetcode.com/problems/combination-sum/)| [Python](https://tinyurl.com/wu6rdaw/39_Combination_Sum.py)| [Article 1](https://tinyurl.com/y5fpdget), [Article 2](https://leetcode.com/problems/combination-sum/discuss/16510/Python-dfs-solution.) | Medium | Backtracking Fundamentals |
523524
|17| [40. Combination Sum II](https://tinyurl.com/roqgnjf)| [Python](https://tinyurl.com/wu6rdaw/40_Combination_Sum_II.py)| --- | Medium | Backtracking Fundamentals |
524525
|18| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Python](https://tinyurl.com/wu6rdaw/17_Letter_Combinations_of_a_Phone_Number.py)| [Video 1](https://www.youtube.com/watch?v=a-sMgZ7HGW0), [Video 2 - paid course](https://codinginterviewclass.com/courses/633601/lectures/11653975), [Article 1](https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8067/Python-easy-to-understand-backtracking-solution.) | Medium | Backtracking Fundamentals |
525-
|19| [22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Python](https://tinyurl.com/wu6rdaw/22_Generate_Parentheses.py)| [educative.io](https://tinyurl.com/wady2xs), [Video 1](https://www.youtube.com/watch?v=sz1qaKt0KGQ), [Article 1](https://leetcode.com/problems/generate-parentheses/discuss/10100/Easy-to-understand-Java-backtracking-solution) | Medium | Backtracking Fundamentals |
526+
|19| [22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Python](https://tinyurl.com/wu6rdaw/22_Generate_Parentheses.py), [Swift](https://tinyurl.com/wuja3c4/22_Generate_Parentheses.swift) | [educative.io](https://tinyurl.com/wady2xs), [Video 1](https://www.youtube.com/watch?v=sz1qaKt0KGQ), [Article 1](https://leetcode.com/problems/generate-parentheses/discuss/10100/Easy-to-understand-Java-backtracking-solution) | Medium | Backtracking Fundamentals |
526527

527528
</p>
528529
</details>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# """
2+
# This is the robot's control interface.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
# class Robot(object):
6+
# def move(self):
7+
# """
8+
# Returns true if the cell in front is open and robot moves into the cell.
9+
# Returns false if the cell in front is blocked and robot stays in the current cell.
10+
# :rtype bool
11+
# """
12+
#
13+
# def turnLeft(self):
14+
# """
15+
# Robot will stay in the same cell after calling turnLeft/turnRight.
16+
# Each turn will be 90 degrees.
17+
# :rtype void
18+
# """
19+
#
20+
# def turnRight(self):
21+
# """
22+
# Robot will stay in the same cell after calling turnLeft/turnRight.
23+
# Each turn will be 90 degrees.
24+
# :rtype void
25+
# """
26+
#
27+
# def clean(self):
28+
# """
29+
# Clean the current cell.
30+
# :rtype void
31+
# """
32+
33+
class Solution(object):
34+
def cleanRoom(self, robot):
35+
"""
36+
:type robot: Robot
37+
:rtype: None
38+
"""
39+
# going clockwise : 0: 'up', 1: 'right', 2: 'down', 3: 'left'
40+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
41+
visited = set()
42+
self.backtrack(robot, (0, 0), 0, directions, visited)
43+
44+
def backtrack(self, robot, cell, currentDirection, directions, visited):
45+
visited.add(cell)
46+
robot.clean()
47+
# going clockwise : 0: 'up', 1: 'right', 2: 'down', 3: 'left'
48+
for i in range(4):
49+
newDirection = (currentDirection + i) % 4
50+
newCell = (cell[0] + directions[newDirection][0], cell[1] + directions[newDirection][1])
51+
if not newCell in visited and robot.move():
52+
self.backtrack(robot, newCell, newDirection, directions, visited)
53+
self.goBack(robot) # go back to the previous cell and on the same direction
54+
# turn the robot following chosen direction : clockwise
55+
robot.turnRight()
56+
57+
def goBack(self, robot):
58+
robot.turnRight()
59+
robot.turnRight()
60+
robot.move()
61+
robot.turnRight()
62+
robot.turnRight()
63+
64+
65+
66+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Foundation
2+

0 commit comments

Comments
 (0)