LeetCode[Hard]------489. Robot Room Cleaner
問題描述
Given a robot cleaner in a room modeled as a grid.
Each cell in the grid can be empty or blocked.
The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.
When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
Design an algorithm to clean the entire room using only the 4 given APIs shown below.
interface Robot {
// returns true if next cell is open and robot moves into the cell.
// returns false if next cell is obstacle and robot stays on the current cell.
boolean move();
// Robot will stay on the same cell after calling turnLeft/turnRight.
// Each turn will be 90 degrees.
void turnLeft();
void turnRight();
// Clean the current cell.
void clean();
}
Example:
Input: room = [ [1,1,1,1,1,0,1,1], [1,1,1,1,1,0,1,1], [1,0,1,1,1,1,1,1], [0,0,0,1,0,0,0,0], [1,1,1,1,1,1,1,1] ], row = 1, col = 3 Explanation: All grids in the room are marked by either 0 or 1. 0 means the cell is blocked, while 1 means the cell is accessible. The robot initially starts at the position of row=1, col=3. From the top left corner, its position is one row below and three columns right.
Notes:
- 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.
- The robot’s initial position will always be in an accessible cell.
- The initial direction of the robot will be facing up.
- All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
- Assume all four edges of the grid are all surrounded by wall.
簡單翻譯一下,給一個二維矩陣,裡面只有0和1兩種數字,0表示這個Node是阻塞的,1表示該Node是暢通的。題目給出了Robot的interface,裡面是4個Robot的Operation。現在問題是設計cleanRoom(Robot robot) 這個方法,使這個Robot能夠clean這個房間中所有暢通的點。
思路:
這是一道典型的DFS題目,我們可以讓robot一直向前掃,直到obstructed。此時我們讓robot換一個方向(turnLeft or turnRight),然後繼續move。直到robot到達一個點,這個點前後左右不是blocked就是visited的時候,backtracking方法返回。我們就用註釋track back後面的5行程式碼找到robot在這個點之前的狀態,繼續嘗試換一個方向move。當所有的backtracking返回,以及for loop執行完畢後,即可clean all the available point。
程式碼:
public void cleanRoom(Robot robot) {
Set<String> visited = new HashSet<>();
backtracking(robot, visited, 0, 0, 0);
}
int[][] dir = {{1,0}, {-1,0}, {0,1}, {0, -1}};
private void backtracking(Robot robot, Set<String> visited, int x, int y, int arrow) {
String path = x + "-" + y;
if (visited.contains(path)) return;
visited.add(path);
robot.clean();
for (int i = 0; i < 4; i++) {
if (robot.move()) {
//go all the way till cannot move, then back one step
int nx = x + dir[arrow][0];
int ny = y + dir[arrow][1];
backtracking(robot, visited, nx, ny, arrow);
//trace back
robot.turnLeft();
robot.turnLeft();
robot.move();
robot.turnLeft();
robot.turnLeft();
}
robot.turnLeft();// or turnRight();
arrow = (arrow + 1) % 4;
}
}