|
| 1 | +/** |
| 2 | +
|
| 3 | + Given a robot cleaner in a room modeled as a grid. |
| 4 | +
|
| 5 | + Each cell in the grid can be empty or blocked. |
| 6 | +
|
| 7 | + The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. |
| 8 | +
|
| 9 | + When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell. |
| 10 | +
|
| 11 | + Design an algorithm to clean the entire room using only the 4 given APIs shown below. |
| 12 | +
|
| 13 | + interface Robot { |
| 14 | + // returns true if next cell is open and robot moves into the cell. |
| 15 | + // returns false if next cell is obstacle and robot stays on the current cell. |
| 16 | + boolean move(); |
| 17 | +
|
| 18 | + // Robot will stay on the same cell after calling turnLeft/turnRight. |
| 19 | + // Each turn will be 90 degrees. |
| 20 | + void turnLeft(); |
| 21 | + void turnRight(); |
| 22 | +
|
| 23 | + // Clean the current cell. |
| 24 | + void clean(); |
| 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 | + */ |
| 46 | +/** |
| 47 | + * // This is the robot's control interface. |
| 48 | + * // You should not implement it, or speculate about its implementation |
| 49 | + * function Robot() { |
| 50 | + * |
| 51 | + * // Returns true if the cell in front is open and robot moves into the cell. |
| 52 | + * // Returns false if the cell in front is blocked and robot stays in the current cell. |
| 53 | + * @return {boolean} |
| 54 | + * this.move = function() { |
| 55 | + * ... |
| 56 | + * }; |
| 57 | + * |
| 58 | + * // Robot will stay in the same cell after calling turnLeft/turnRight. |
| 59 | + * // Each turn will be 90 degrees. |
| 60 | + * @return {void} |
| 61 | + * this.turnLeft = function() { |
| 62 | + * ... |
| 63 | + * }; |
| 64 | + * |
| 65 | + * // Robot will stay in the same cell after calling turnLeft/turnRight. |
| 66 | + * // Each turn will be 90 degrees. |
| 67 | + * @return {void} |
| 68 | + * this.turnRight = function() { |
| 69 | + * ... |
| 70 | + * }; |
| 71 | + * |
| 72 | + * // Clean the current cell. |
| 73 | + * @return {void} |
| 74 | + * this.clean = function() { |
| 75 | + * ... |
| 76 | + * }; |
| 77 | + * }; |
| 78 | + */ |
| 79 | +/** |
| 80 | + * @param {Robot} robot |
| 81 | + * @return {void} |
| 82 | + */ |
| 83 | + |
| 84 | +/** |
| 85 | + * |
| 86 | + * O(n) time |
| 87 | + * O(n) space |
| 88 | + * |
| 89 | + * Runtime: 76 ms, faster than 98.80% |
| 90 | + */ |
| 91 | +var cleanRoom = function(robot) { |
| 92 | + const doneSet = new Set(); |
| 93 | + // [x, y] up, right, down, left |
| 94 | + const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]; |
| 95 | + |
| 96 | + dfs(0, 0, 0); |
| 97 | + |
| 98 | + function dfs(lastDirIndex, x, y) { |
| 99 | + // move -> clean -> add to doneSet [x:y] |
| 100 | + robot.clean(); |
| 101 | + doneSet.add(`${x}:${y}`); |
| 102 | + // dfs 4 direction (if not visited) |
| 103 | + for (let i = 0; i < 4; i++) { |
| 104 | + let currDirIndex = (lastDirIndex + i) % 4; |
| 105 | + let xx = x + dirs[currDirIndex][0]; |
| 106 | + let yy = y + dirs[currDirIndex][1]; |
| 107 | + |
| 108 | + // if the facing grid not cleaned yet |
| 109 | + // try to move to the facing grid now |
| 110 | + if (!doneSet.has(`${xx}:${yy}`) && robot.move()) { |
| 111 | + dfs(currDirIndex, xx, yy); |
| 112 | + } |
| 113 | + |
| 114 | + robot.turnRight(); // turn right 4 time so we face the same direction as we get here |
| 115 | + } |
| 116 | + |
| 117 | + // we have done 4 directions here, move back to where we come from |
| 118 | + robot.turnRight(); |
| 119 | + robot.turnRight(); |
| 120 | + robot.move(); |
| 121 | + robot.turnRight(); |
| 122 | + robot.turnRight(); |
| 123 | + } |
| 124 | +}; |
0 commit comments