LeetCode 489. Robot Room Cleaner

原题链接在这里:https://leetcode.com/problems/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:

  1. 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.
  2. The robot's initial position will always be in an accessible cell.
  3. The initial direction of the robot will be facing up.
  4. All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
  5. Assume all four edges of the grid are all surrounded by wall.

题解:

For the robot itself, it doesn't know the grid board. But it could treat its current location as (0, 0).

For current direction, if robot could move forward, continue dfs on next grid, then backtracking. Then change to next direction.

If it could not more forward, change to next direction.

Have a visited set to record visited coordnates. It the it has visited this coordnates, simply return.

Time Complexity: O(mn). m = grid.length. n = grid[0].length.

Space: O(m*n). stack space.

AC Java:

 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     int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
20     
21     public void cleanRoom(Robot robot) {
22         HashSet visited = new HashSet<>();
23         dfsClean(robot, 0, 0, 0, visited);
24     }
25     
26     private void dfsClean(Robot robot, int x, int y, int curD, Set visited){
27         String coord = x + "," + y;
28         if(visited.contains(coord)){
29             return;
30         }
31         
32         visited.add(coord);
33         robot.clean();
34         for(int i = 0; i<4; i++){
35             if(robot.move()){
36                 int dx = x + dirs[curD][0];
37                 int dy = y + dirs[curD][1];
38                 dfsClean(robot, dx, dy, curD, visited);
39                 
40                 // backtracking
41                 robot.turnLeft();
42                 robot.turnLeft();
43                 robot.move();
44                 robot.turnLeft();
45                 robot.turnLeft();
46             }
47             
48             robot.turnRight();
49             curD = (curD + 1) % 4;
50         }
51     }
52 }

类似Walls and Gates.

你可能感兴趣的:(LeetCode 489. Robot Room Cleaner)