489. 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:
- 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.
- The robot's initial position will always be in an accessible cell.
- The initial direction of the robot will be facing up.
- All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
- Assume all four edges of the grid are all surrounded by wall.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
想不出还要回到原来的位置,而且如果不move的话要向右转。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
start cleaning然后向四周扩展,能move就move,否则向右转。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 判断方向是否重复,要把现有方向添加到SET中来判断
robot.clean();
set.add(temp);
- 方向的变化是X Y的变化
[二刷]:
This is actually the standard backtracking method. Here is an example, in this problem, suppose the robot starts at (0, 0), and all four directions are accessible. You can assume the robots starts at a crossroad so (0, 0) is the only point to connect other roads. First, it goes up to (-1, 0) and continues from that cell. After all the upper cells are cleaned, it should return to (0, 0), then go right to (0, 1), and so on. Those lines of code make the robot go back to (0, 0) from (-1, 0).
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
backtracing的入口在for循环之前
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
489. Robot Room Cleaner扫地机器人的更多相关文章
- [LeetCode] 489. Robot Room Cleaner 扫地机器人
Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...
- [LeetCode] Robot Room Cleaner 扫地机器人
Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...
- LeetCode 489. Robot Room Cleaner
原题链接在这里:https://leetcode.com/problems/robot-room-cleaner/ 题目: Given a robot cleaner in a room modele ...
- 【BZOJ5318】[JSOI2018]扫地机器人(动态规划)
[BZOJ5318][JSOI2018]扫地机器人(动态规划) 题面 BZOJ 洛谷 题解 神仙题.不会.... 先考虑如果一个点走向了其下方的点,那么其右侧的点因为要被访问到,所以必定只能从其右上方 ...
- Hihocoder 1275 扫地机器人 计算几何
题意: 有一个房间的形状是多边形,而且每条边都平行于坐标轴,按顺时针给出多边形的顶点坐标 还有一个正方形的扫地机器人,机器人只可以上下左右移动,不可以旋转 问机器人移动的区域能不能覆盖整个房间 分析: ...
- Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner
D. Robot Vacuum Cleaner time limit per test 1 second memory limit per test 256 megabytes Problem Des ...
- CodeForces - 922D Robot Vacuum Cleaner (贪心)
Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- Java实现第十届蓝桥杯JavaC组第十题(试题J)扫地机器人
扫地机器人 时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分 [问题描述] 小明公司的办公区有一条长长的走廊,由 N 个方格区域组成,如下图所 示. 走廊内部署了 K 台扫地机器人 ...
随机推荐
- c# Type.InvokeMember用法
函数原型: public object InvokeMember(string, BindingFlags, Binder, object, object[]);string:你所要调用的函数名Bin ...
- 解决Mac上安装mysqlclient的错误
要想使用python操作mysql,那么就需要安装python操作数据库的驱动,由于mysqldb不支持python3,我选择安装mysqlclient, 命令行输入:pip3 install ...
- 处于ESTABLISHED 状态的socket 却没有进程信息
接<一次docker中的nginx进程响应慢问题定位记录> 在排查这个问题的时候,我先使用netstat 去查看,看到底是内核协议栈的连接请求没给到进程,还是进程accept链路慢了,或者 ...
- java为什么有些异常throw出去需要在函数头用throws声明,一些就不用。
Excepiton分两类:checked exception.runtime exception:直接继承自Exception就是checked exception,继承自RuntimeExcepti ...
- 对String值不可变的理解以及String类型的引用传递问题
今天复习java时,突然注意到了一句以前没有注意过的一句话,String 是final修饰的,其值是不可变的.当时看的一脸懵逼,String str = "abc"; str = ...
- ReactiveX 学习笔记(4)过滤数据流
Filtering Observables 本文主题为过滤 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(三)Filtering Deb ...
- bat脚本基础教程
bat脚本就是DOS批处理脚本,就是将一系列DOS命令按照一定顺序排列而形成的集合,运行在windows命令行环境上.本文主要介绍bat脚本基础语法,希望完成本文内容学习之后具备基础的bat脚本开发能 ...
- tomcat 简单安装配置说明
1.下载安装文件[apache-tomcat-6.0.41.tar.gz]到指定目录[/usr/local]2.解压文件[tar -zxvf apache-tomcat-6.0.41.tar.gz], ...
- 【原】wow64 x86/x64 代码切换过程分析
下面以ntdll32!ZwQueryInformationProcess API为例分析 x86代码与x64代码之间的切换过程, 32bit的test程序: step1: ntdll32!ZwQuer ...
- getElementsByTagName
1.getElementsByTagName * document.getelementByID(id名称)* 通过id名称获取元素,它只有一个主语,docment(整个文档)** getElemne ...