Leetcode: 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.
  1. To track the cells the robot has cleaned, start a index pair (i, j) from (0, 0). When go up, i-1; go down, i+1; go left, j-1; go right: j+1.
  2. Also use arrow to record the current direction of the robot. arrow = (arrow + 1) % 4 correspond to to the dirs array
 1 /**
 2  * // This is the robot's control interface.
 3  * // You should not implement it, or speculate about its implementation
 4  * interface Robot {
 5  *     // Returns true if the cell in front is open and robot moves into the cell.
 6  *     // Returns false if the cell in front is blocked and robot stays in the current cell.
 7  *     public boolean move();
 8  *
 9  *     // Robot will stay in the same cell after calling turnLeft/turnRight.
10  *     // Each turn will be 90 degrees.
11  *     public void turnLeft();
12  *     public void turnRight();
13  *
14  *     // Clean the current cell.
15  *     public void clean();
16  * }
17  */
18 class Solution {
19     public void cleanRoom(Robot robot) {
20         HashSet<String> visited = new HashSet<>();
21         dfs(robot, visited, 0, 0, 0);
22     }
23     
24     int[][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
25     
26     public void dfs(Robot robot, HashSet<String> visited, int i, int j, int arrow) {
27         if (visited.contains(i + "-" + j)) return;
28         visited.add(i + "-" + j);
29         robot.clean();
30         
31         for (int k = 0; k < 4; k ++) {
32             //go all the way till cannot move, then back one step
33             if (robot.move()) { // this check if next cell is ok, if yes, it moves in already
34                 int newX = i + dirs[arrow][0]; // this is next cell position, already in there
35                 int newY = j + dirs[arrow][1];
36                 dfs(robot, visited, newX, newY, arrow);
37                 
38                 // backtrack: reverse and go back to the last position
39                 robot.turnLeft();  
40                 robot.turnLeft();
41                 robot.move();
42                 robot.turnRight();
43                 robot.turnRight();
44             }
45             robot.turnRight();
46             arrow = (arrow + 1) % 4;
47         }
48     }
49 }

 

Guess you like

Origin www.cnblogs.com/EdwardLiu/p/11664743.html