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:
简单翻译一下,给一个二维矩阵,里面只有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;
}
}
仅用于个人的学习理解,如需转载,请标明出处:
https://blog.csdn.net/sc19951007/article/details/83743171