|
| 1 | +/** |
| 2 | + * 489. Robot Room Cleaner |
| 3 | + * https://leetcode.com/problems/robot-room-cleaner/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are controlling a robot that is located somewhere in a room. The room is modeled as an m x n |
| 7 | + * binary grid where 0 represents a wall and 1 represents an empty slot. |
| 8 | + * |
| 9 | + * The robot starts at an unknown location in the room that is guaranteed to be empty, and you do |
| 10 | + * not have access to the grid, but you can move the robot using the given API Robot. |
| 11 | + * |
| 12 | + * You are tasked to use the robot to clean the entire room (i.e., clean every empty cell in the |
| 13 | + * room). The robot with the four given APIs can move forward, turn left, or turn right. Each turn |
| 14 | + * is 90 degrees. |
| 15 | + * |
| 16 | + * When the robot tries to move into a wall cell, its bumper sensor detects the obstacle, and it |
| 17 | + * stays on the current cell. |
| 18 | + * |
| 19 | + * Design an algorithm to clean the entire room using the following APIs: |
| 20 | + * |
| 21 | + * interface Robot { |
| 22 | + * // returns true if next cell is open and robot moves into the cell. |
| 23 | + * // returns false if next cell is obstacle and robot stays on the current cell. |
| 24 | + * boolean move(); |
| 25 | + * |
| 26 | + * // Robot will stay on the same cell after calling turnLeft/turnRight. |
| 27 | + * // Each turn will be 90 degrees. |
| 28 | + * void turnLeft(); |
| 29 | + * void turnRight(); |
| 30 | + * |
| 31 | + * // Clean the current cell. |
| 32 | + * void clean(); |
| 33 | + * } |
| 34 | + * |
| 35 | + * Note that the initial direction of the robot will be facing up. You can assume all four edges of |
| 36 | + * the grid are all surrounded by a wall. |
| 37 | + * |
| 38 | + * Custom testing: |
| 39 | + * The input is only given to initialize the room and the robot's position internally. You must |
| 40 | + * solve this problem "blindfolded". In other words, you must control the robot using only the |
| 41 | + * four mentioned APIs without knowing the room layout and the initial robot's position. |
| 42 | + */ |
| 43 | + |
| 44 | +/** |
| 45 | + * @param {Robot} robot |
| 46 | + * @return {void} |
| 47 | + */ |
| 48 | +var cleanRoom = function(robot) { |
| 49 | + const visited = new Set(); |
| 50 | + const directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]; |
| 51 | + |
| 52 | + backtrack(0, 0, 0); |
| 53 | + |
| 54 | + function backtrack(row, col, dir) { |
| 55 | + const key = `${row},${col}`; |
| 56 | + if (visited.has(key)) return; |
| 57 | + |
| 58 | + visited.add(key); |
| 59 | + robot.clean(); |
| 60 | + |
| 61 | + for (let i = 0; i < 4; i++) { |
| 62 | + const newDir = (dir + i) % 4; |
| 63 | + const [dx, dy] = directions[newDir]; |
| 64 | + const newRow = row + dx; |
| 65 | + const newCol = col + dy; |
| 66 | + |
| 67 | + if (robot.move()) { |
| 68 | + backtrack(newRow, newCol, newDir); |
| 69 | + robot.turnRight(); |
| 70 | + robot.turnRight(); |
| 71 | + robot.move(); |
| 72 | + robot.turnLeft(); |
| 73 | + robot.turnLeft(); |
| 74 | + } |
| 75 | + robot.turnRight(); |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
0 commit comments