wangcy6 / leetcode

LeetCode Problems' Solutions
5 stars 1 forks source link

[每日一题]-2020年1月15日 -489. Robot Room Cleaner 扫地机器人 #15

Open watchpoints opened 4 years ago

watchpoints commented 4 years ago
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.

给一个扫地机器人和一个用网格表示的屋子,每个格子是0或者1, 0表示是障碍物,1表示可以通过。机器人有move() ,turnLeft(),turnRight(),clean() 4个API,设计一个算法把整个屋子扫一遍。

注意:房间的布局和机器人的初始位置都是不知道的。机器人的初始位置一定在可以通过的格子里。机器人的初始方向是向上。所以可以通过的格子是联通的。假设网格的四周都是墙。

watchpoints commented 4 years ago

视频:https://www.youtube.com/watch?v=xOoXY_D1UJk 开始:2020年1月15日 结束: