LeetCode 489. Robot Room Cleaner
原题链接在这里: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:
- 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.
题解:
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;
}
}
}
LeetCode 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 ...
- 489. Robot Room Cleaner扫地机器人
[抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...
- 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 ...
- [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 - 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 1041. Robot Bounded In Circle (困于环中的机器人)
题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...
- LeetCode 657. Robot Return to Origin
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
随机推荐
- asp.net core 3.0 身份认证 替换为自已的提供程序 AuthenticationStateProvider replace to SelfAuthenticationStateProvider
public void ConfigureServices(IServiceCollection services) { // 添加身份验证服务 services.AddAuthorizationCo ...
- Redis学习之ziplist压缩列表源码分析
一.压缩列表ziplist在redis中的应用 1.做列表键 当一个列表键只包含少量列表项,并且每个列表项要么是小整数,要么是短字符串,那么redis会使用压缩列表作为列表键的底层实现 2.哈希键 当 ...
- 利用开源项目 FFMpegSharp 实现音视频提取、转码、抓图等操作
开源项目地址:https://github.com/vladjerca/FFMpegSharp 首先需要在 web.config 或 app.config 中配置 <appSettings> ...
- 前端学习:CSS的学习总结(图解)
前端学习:CSS的学习总结(图解) CSS代码笔记 CSS简介 css的引入方式和书写规范 CSS选择器 CSS属性 CSS盒子模型 CSS的定位
- C#利用newtonsoft.json读取.so配置文件内容
今天花 了点时间来使用 C#读取json文件 ,文件后缀为 .so文件 ,也是基于文件流的形式 获取 对象 ,然后解析; 之所以尝试 使用 json读取 ,是因为其配置文件的格式 更为友好 和方便,直 ...
- svg图片拖动与缩放
引入jquery.js文件,svg-pan-zoom.min.js文件 和 hammer.min.js 文件 这三个文件可以在网上搜一下下载 //svg拖动和缩放 initPanZoom() { th ...
- centos7 配置nginx vim语法高亮
看了Nginx核心知识100讲,按照他的做法,没有配置成功,可以使用下面的方法: 下载nginx源码,http://nginx.org/en/download.html 这里下载的是:nginx-1. ...
- python 进程和线程-进程和线程的比较以及分布式进程
进程和线程的比较 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017631469467456 我们介绍了多进程和多线程,这是实现多任 ...
- Java I/O系统学习系列二:输入和输出
编程语言的I/O类库中常使用流这个抽象概念,它代表任何有能力产出数据的数据源对象或者是有能力接收数据的接收端对象.“流”屏蔽了实际的I/O设备中处理数据的细节. 在这个系列的第一篇文章:<< ...
- bundler-sfm windows下编译过程中出现的错误
一.“sysdep1.h”文件缺失 错误提示: fatal error C1083: 无法打开包括文件: “sysdep1.h”: No such file or directory 这些作为这个软件 ...