原题链接在这里: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.

Note: every time curD + 1, not curD + i.

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

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

AC Java:

 /**
* // 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 {
int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public void cleanRoom(Robot robot) {
HashSet<String> visited = new HashSet<>();
dfsClean(robot, 0, 0, 0, visited);
} private void dfsClean(Robot robot, int x, int y, int curD, Set<String> visited){
String coord = x + "," + y;
if(visited.contains(coord)){
return;
} visited.add(coord);
robot.clean();
for(int i = 0; i<4; i++){
if(robot.move()){
int dx = x + dirs[curD][0];
int dy = y + dirs[curD][1];
dfsClean(robot, dx, dy, curD, visited); // backtracking
robot.turnLeft();
robot.turnLeft();
robot.move();
robot.turnLeft();
robot.turnLeft();
} robot.turnRight();
curD = (curD + 1) % 4;
}
}
}

类似Walls and Gates.

最新文章

  1. Ajax的实现
  2. 70. Climbing Stairs
  3. MySQL--索引条件下推优化
  4. Codeforces 374A - Inna and Pink Pony
  5. javascript运算符整理
  6. c语言:将二进制数按位输出
  7. poj3061尺取法
  8. C++ 中的一些错觉
  9. 剑指Offer-构建乘积数组
  10. Java辅助类持续汇总~
  11. ASP.NET gridview导出excel,防止繁体产生有乱码的方式
  12. 关于pandas 调用mongodb出Memory error错误
  13. SQL对于 小数处理的小结
  14. day 7-6 GIL,死锁,递归锁与信号量,Event,queue,
  15. C# 如何物理删除有主外键约束的记录?存储过程实现
  16. 线性代数的本质与几何意义 03. 矩阵与线性变换 (3blue1brown 咪博士 图文注解版)
  17. loadrunner controller如何执行测试
  18. 【Spring学习笔记-MVC】Spring MVC之多文件上传 (zhan)
  19. php -- 表单多选
  20. HUE配置文件hue.ini 的yarn_clusters模块详解(图文详解)(分HA集群和非HA集群)

热门文章

  1. C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)
  2. axios浏览器异步请求方法封装 XMLHttpRequest
  3. Linux 安装Mysql1.8【yum安装】
  4. 基础知识---const、readonly、static
  5. Password file not found:.../jmxremote.password
  6. [原创]Spring-Security-Oauth2.0浏览器端的登录项目分享
  7. 【问题记录】ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
  8. SpringBoot 通过jjwt快速实现token授权
  9. linux centos无法删除网站根目录下的.user.ini解决办法
  10. 在centos下安装rar解压.rar压缩包