489. Robot Room Cleaner
class Solution:
def cleanRoom(self, robot):
def goBack():
robot.turnRight()
robot.turnRight()
robot.move()
robot.turnRight()
robot.turnRight()
visited = set()
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
def backtrack(cell = (0, 0), d = 0):
visited.add(cell)
robot.clean()
for i in range(4):
newD = (i + d) % 4
newCell = (cell[0] + directions[newD][0], cell[1] + directions[newD][1])
if newCell not in visited and robot.move():
backtrack(newCell, newD)
goBack()
robot.turnRight()
backtrack()