|
| 1 | +# QUESTION |
| 2 | +Given a robot cleaner in a room modeled as a grid. |
| 3 | + |
| 4 | +Each cell in the grid can be empty or blocked. |
| 5 | + |
| 6 | +The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. |
| 7 | + |
| 8 | +When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell. |
| 9 | + |
| 10 | +Design an algorithm to clean the entire room using only the 4 given APIs shown below. |
| 11 | +``` |
| 12 | +interface Robot { |
| 13 | + // returns true if next cell is open and robot moves into the cell. |
| 14 | + // returns false if next cell is obstacle and robot stays on the current cell. |
| 15 | + boolean move(); |
| 16 | +
|
| 17 | + // Robot will stay on the same cell after calling turnLeft/turnRight. |
| 18 | + // Each turn will be 90 degrees. |
| 19 | + void turnLeft(); |
| 20 | + void turnRight(); |
| 21 | +
|
| 22 | + // Clean the current cell. |
| 23 | + void clean(); |
| 24 | +} |
| 25 | +``` |
| 26 | +Example: |
| 27 | +``` |
| 28 | +Input: |
| 29 | +room = [ |
| 30 | + [1,1,1,1,1,0,1,1], |
| 31 | + [1,1,1,1,1,0,1,1], |
| 32 | + [1,0,1,1,1,1,1,1], |
| 33 | + [0,0,0,1,0,0,0,0], |
| 34 | + [1,1,1,1,1,1,1,1] |
| 35 | +], |
| 36 | +row = 1, |
| 37 | +col = 3 |
| 38 | +
|
| 39 | +Explanation: |
| 40 | +All grids in the room are marked by either 0 or 1. |
| 41 | +0 means the cell is blocked, while 1 means the cell is accessible. |
| 42 | +The robot initially starts at the position of row=1, col=3. |
| 43 | +From the top left corner, its position is one row below and three columns right. |
| 44 | +``` |
| 45 | +Notes: |
| 46 | +- 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. |
| 47 | +- The robot's initial position will always be in an accessible cell. |
| 48 | +- The initial direction of the robot will be facing up. |
| 49 | +- All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot. |
| 50 | +- Assume all four edges of the grid are all surrounded by wall. |
| 51 | + |
| 52 | +# EXPLANATION |
| 53 | +This question is testing your knowledge in writing clean code while using someone else's API and your understanding of DFS/stack. |
| 54 | +This question can get very messy fast, so its best to keep it simple. |
| 55 | + |
| 56 | +First, noticed that your robot will be placed in a room/grid at random. Instead, think about how you would clean/visit all the cells in the grid if you did know your coordinates? |
| 57 | +You should think about DFS, BFS however would be more difficult to implement. As with any DFS, you would need a visited set to avoid cycles. |
| 58 | + |
| 59 | +Now lets consider the fact that your robot doesn't know where its coordinates are at. Can we instead use arbitrary coordinates? |
| 60 | +Say we always start at (0,0). Then base on our direction, say we pick an arbitrary direction, up. We attempt to move up to (0,1) based on our current direction. |
| 61 | +If that doesn't work, we rotate to the right and try (1,0). For each cell we will attempt to try the next cell in a clockwise manner, counter-clockwise can also work. |
| 62 | + |
| 63 | +Now the next question is, what happens after we have tried all four cells. Our robot should then return to the original cell it came from. |
| 64 | + |
| 65 | +It is also important to note that when your robot leaves a cell, its direction is different from when it leaves the cell then when it comes back. So you need to reset its direction. |
| 66 | + |
| 67 | +# SOLUTION |
| 68 | +``` |
| 69 | +# """ |
| 70 | +# This is the robot's control interface. |
| 71 | +# You should not implement it, or speculate about its implementation |
| 72 | +# """ |
| 73 | +#class Robot(object): |
| 74 | +# def move(self): |
| 75 | +# """ |
| 76 | +# Returns true if the cell in front is open and robot moves into the cell. |
| 77 | +# Returns false if the cell in front is blocked and robot stays in the current cell. |
| 78 | +# :rtype bool |
| 79 | +# """ |
| 80 | +# |
| 81 | +# def turnLeft(self): |
| 82 | +# """ |
| 83 | +# Robot will stay in the same cell after calling turnLeft/turnRight. |
| 84 | +# Each turn will be 90 degrees. |
| 85 | +# :rtype void |
| 86 | +# """ |
| 87 | +# |
| 88 | +# def turnRight(self): |
| 89 | +# """ |
| 90 | +# Robot will stay in the same cell after calling turnLeft/turnRight. |
| 91 | +# Each turn will be 90 degrees. |
| 92 | +# :rtype void |
| 93 | +# """ |
| 94 | +# |
| 95 | +# def clean(self): |
| 96 | +# """ |
| 97 | +# Clean the current cell. |
| 98 | +# :rtype void |
| 99 | +# """ |
| 100 | +
|
| 101 | +""" |
| 102 | + (0,1) |
| 103 | +(-1,0) (0,0) (1,0) |
| 104 | + (0,-1) |
| 105 | +""" |
| 106 | +
|
| 107 | +class Solution(object): |
| 108 | + def __init__(self): |
| 109 | + # represents up, right, down, left directions |
| 110 | + self.dirs = [(0,1), (1,0), (0,-1), (-1,0)] |
| 111 | +
|
| 112 | + # current direction of the roomba based on an arbitrary direction 'up' |
| 113 | + self.curr_dir = self.dirs[0] |
| 114 | + |
| 115 | + # current arbitrary cell |
| 116 | + self.curr_pos = (0,0) |
| 117 | + |
| 118 | + # cleaned cells |
| 119 | + self.visited_set = set() |
| 120 | + |
| 121 | + def cleanRoom(self, robot): |
| 122 | + """ |
| 123 | + :type robot: Robot |
| 124 | + :rtype: None |
| 125 | + """ |
| 126 | + self.dfs_clean(robot) |
| 127 | +
|
| 128 | + def dfs_clean(self, robot): |
| 129 | + robot.clean() |
| 130 | + curr_pos = self.curr_pos |
| 131 | + self.visited_set.add(self.curr_pos) |
| 132 | + |
| 133 | + # continue moving forward |
| 134 | + self.move_forward(robot) |
| 135 | + |
| 136 | + # right |
| 137 | + self.rotate_right(robot) |
| 138 | + self.move_forward(robot) |
| 139 | + |
| 140 | + # bottom |
| 141 | + self.rotate_right(robot) |
| 142 | + self.move_forward(robot) |
| 143 | + |
| 144 | + # left |
| 145 | + self.rotate_right(robot) |
| 146 | + self.move_forward(robot) |
| 147 | + |
| 148 | + # back track and reset direction |
| 149 | + self.rotate_right(robot) |
| 150 | + self.rotate_right(robot) |
| 151 | + self.rotate_right(robot) |
| 152 | + self.move_back(robot) |
| 153 | + self.rotate_right(robot) |
| 154 | + self.rotate_right(robot) |
| 155 | + |
| 156 | + def move_back(self, robot): |
| 157 | + next_pos = self.get_next_pos() |
| 158 | + if robot.move(): |
| 159 | + self.curr_pos = next_pos |
| 160 | + |
| 161 | + def move_forward(self, robot): |
| 162 | + next_pos = self.get_next_pos() |
| 163 | + if next_pos not in self.visited_set: |
| 164 | + if robot.move(): |
| 165 | + self.curr_pos = next_pos |
| 166 | + self.dfs_clean(robot) |
| 167 | + else: # hit a wall |
| 168 | + self.visited_set.add(next_pos) |
| 169 | + |
| 170 | + def get_next_pos(self): |
| 171 | + next_pos = (self.curr_pos[0]+self.curr_dir[0], self.curr_pos[1]+self.curr_dir[1]) |
| 172 | + return next_pos |
| 173 | + |
| 174 | + def rotate_right(self, robot): |
| 175 | + for index, direction in enumerate(self.dirs): |
| 176 | + if direction == self.curr_dir: |
| 177 | + if index >= len(self.dirs)-1: |
| 178 | + self.curr_dir = self.dirs[0] |
| 179 | + else: |
| 180 | + self.curr_dir = self.dirs[index+1] |
| 181 | + break |
| 182 | + robot.turnRight() |
| 183 | +``` |
0 commit comments