首页 > 其他分享 >[Google] LeetCode 489 Robot Room Cleaner

[Google] LeetCode 489 Robot Room Cleaner

时间:2022-08-31 17:23:04浏览次数:64  
标签:Cleaner Google Room void move Robot robot cell room

You are controlling a robot that is located somewhere in a room. The room is modeled as an m x n binary grid where 0 represents a wall and 1 represents an empty slot.

The robot starts at an unknown location in the room that is guaranteed to be empty, and you do not have access to the grid, but you can move the robot using the given API Robot.

You are tasked to use the robot to clean the entire room (i.e., clean every empty cell in the room). The robot with the four given APIs can move forward, turn left, or turn right. Each turn is 90 degrees.

When the robot tries to move into a wall cell, its bumper sensor detects the obstacle, and it stays on the current cell.

Design an algorithm to clean the entire room using the following APIs:

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();
}

Note that the initial direction of the robot will be facing up. You can assume all four edges of the grid are all surrounded by a wall.

Custom testing:

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 four mentioned APIs without knowing the room layout and the initial robot's position.

Solution

我们利用 \(DFS\) 来求解。

点击查看代码
/**
 * // This is the robot's control interface.
 * // You should not implement it, or speculate about its implementation
 * class Robot {
 *   public:
 *     // 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.
 *     bool move();
 *
 *     // Robot will stay in the same cell after calling turnLeft/turnRight.
 *     // Each turn will be 90 degrees.
 *     void turnLeft();
 *     void turnRight();
 *
 *     // Clean the current cell.
 *     void clean();
 * };
 */

class Solution {
private:
    int dir[4][2]={1,0, 0,1, -1,0, 0,-1};
    map<vector<int>, bool> vis;
    
    void backtrace(Robot& robot){
        robot.turnLeft();robot.turnLeft();
        robot.move();
        robot.turnRight();robot.turnRight();
    }
    
    void dfs(int x, int y, int curdir, Robot& robot){
        vis[{x,y}]=true;
        
        robot.clean();
        
        for(int i=0;i<4;i++){
            int nxtdir = (curdir+i)%4;
            int nx = x+dir[nxtdir][0];
            int ny = y+dir[nxtdir][1];
            
            if(!vis[{nx,ny}] && robot.move()){
                dfs(nx,ny,nxtdir, robot);
                backtrace(robot);
            }
            robot.turnLeft();
        }
    }
    
    
public:
    void cleanRoom(Robot& robot) {
        dfs(0,0,0,robot);
    }
};




标签:Cleaner,Google,Room,void,move,Robot,robot,cell,room
From: https://www.cnblogs.com/xinyu04/p/16643826.html

相关文章