-
Notifications
You must be signed in to change notification settings - Fork 17
/
robot-room-cleaner.py
65 lines (58 loc) · 1.9 KB
/
robot-room-cleaner.py
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
# """
# This is the robot's control interface.
# You should not implement it, or speculate about its implementation
# """
#class Robot:
# def move(self):
# """
# 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.
# :rtype bool
# """
#
# def turnLeft(self):
# """
# Robot will stay in the same cell after calling turnLeft/turnRight.
# Each turn will be 90 degrees.
# :rtype void
# """
#
# def turnRight(self):
# """
# Robot will stay in the same cell after calling turnLeft/turnRight.
# Each turn will be 90 degrees.
# :rtype void
# """
#
# def clean(self):
# """
# Clean the current cell.
# :rtype void
# """
class Solution:
def cleanRoom(self, robot):
"""
:type robot: Robot
:rtype: None
"""
def turn_left(direction, robot):
robot.turnLeft()
return direction[1], -direction[0]
def turn_right(direction, robot):
robot.turnRight()
return -direction[1], direction[0]
def dfs(robot, visited, row, col, direction):
visited.add((row, col))
robot.clean()
for _ in range(4):
next_row, next_col = row + direction[0], col + direction[1]
if (next_row, next_col) not in visited and robot.move():
direction = dfs(robot, visited, next_row, next_col, direction)
direction = turn_left(direction, robot)
else:
direction = turn_right(direction, robot)
direction = turn_left(direction, robot)
direction = turn_left(direction, robot)
robot.move()
return direction
dfs(robot, set(), 0, 0, (1, 0))