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.

 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 
19 class Solution {
20     private static final int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
21     
22     public void cleanRoom(Robot robot) {
23         clean(robot, 0, 0, 0, new HashSet<>());
24     }
25     
26     private void clean(Robot robot, int x, int y, int curDirection, Set<String> visited) {
27         robot.clean();
28         visited.add(x + " " + y);
29         
30         for (int i = 0; i < 4; i++) {
31             int newDirection = (curDirection + i) % 4;
32             int nx = directions[newDirection][0] + x;
33             int ny = directions[newDirection][1] + y;
34             if (!visited.contains(nx + " " + ny) && robot.move()) {
35                 clean(robot, nx, ny, newDirection, visited);
36                 moveBack(robot);
37             }
38             // Changed orientation.
39             robot.turnRight();
40         }
41     }
42     private void moveBack(Robot robot) {
43         robot.turnRight();
44         robot.turnRight();
45         robot.move();
46         //keep the direction it moved to this cell
47         robot.turnRight();
48         robot.turnRight();
49     }
50 }

 

posted @ 2021-03-29 04:32  北叶青藤  阅读(53)  评论(0编辑  收藏  举报