Skip to content

Commit 6ca92e6

Browse files
committed
Add solution #489
1 parent c392b12 commit 6ca92e6

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@
477477
486|[Predict the Winner](./solutions/0486-predict-the-winner.js)|Medium|
478478
487|[Max Consecutive Ones II](./solutions/0487-max-consecutive-ones-ii.js)|Medium|
479479
488|[Zuma Game](./solutions/0488-zuma-game.js)|Hard|
480+
489|[Robot Room Cleaner](./solutions/0489-robot-room-cleaner.js)|Hard|
480481
491|[Non-decreasing Subsequences](./solutions/0491-non-decreasing-subsequences.js)|Medium|
481482
492|[Construct the Rectangle](./solutions/0492-construct-the-rectangle.js)|Easy|
482483
493|[Reverse Pairs](./solutions/0493-reverse-pairs.js)|Hard|

solutions/0489-robot-room-cleaner.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)