LeetCode[Hard]------489. Robot Room Cleaner

版权声明:版权归博主CodingArtist所有,转载请标明出处。 https://blog.csdn.net/sc19951007/article/details/83743171

问题描述

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.

简单翻译一下,给一个二维矩阵,里面只有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

猜你喜欢

转载自blog.csdn.net/sc19951007/article/details/83743171