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.

思路:这题比较难想的是,怎么找下一个方向,不能random的找,否则会陷入死循环,方法就是四个方向按照一定顺序,也就是clockwise的找,下一层的时候,收集可以move到的点,然后再返回,如果不行,机器人就turnRight(),也就是找到下一个pos,跟dx dy是一致的方向;

/**
 * // This is the robot's control interface.
 * // You should not implement it, or speculate about its implementation
 * interface Robot {
 *     // Returns true if the cell in front is open and robot moves into the cell.
 *     // Returns false if the cell in front is blocked and robot stays in the current cell.
 *     public boolean move();
 *
 *     // Robot will stay in the same cell after calling turnLeft/turnRight.
 *     // Each turn will be 90 degrees.
 *     public void turnLeft();
 *     public void turnRight();
 *
 *     // Clean the current cell.
 *     public void clean();
 * }
 */

class Solution {
    // 
    private int[] dx = {-1,0,1,0};
    private int[] dy = {0,1,0,-1};
    
    public void cleanRoom(Robot robot) {
        HashSet<String> visited = new HashSet<>();
        dfs(robot, visited, 0, 0, 0);
    }
    
    private void dfs(Robot robot, HashSet<String> visited,
                    int x, int y, int dir) {
        String pos = x + "-" + y;
        robot.clean();
        visited.add(pos);
        
        for(int k = 0; k < 4; k++) {
            int newdir = (dir + k) % 4;
            int nx = x + dx[newdir];
            int ny = y + dy[newdir];
            String newpos = nx + "-" + ny;
            if(!visited.contains(newpos) && robot.move()) {
                dfs(robot, visited, nx, ny, newdir);
                // back tracking;
                robot.turnLeft();
                robot.turnLeft();
                robot.move();
                robot.turnRight();
                robot.turnRight();
            } 
            // 因为dx,dy是顺时针的,那么向右转,其实是面对下一个方向;
            robot.turnRight();
            
        }
    }
}
发布了710 篇原创文章 · 获赞 13 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105407731