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 ...
随机推荐
- WindowsServer -------------部署软件
1.windowsServer 中创建 lls 在服务器中创建lls 参考 将文件扩展名调出 2.安装数据库 将数据库传递到服务器中 安装sqlserver 数据库 3.在系统中创建文件存 ...
- springBoot获取@NotBlank,@NotNull注解的message信息
概述 springBoot后台验证接收的参数是否不合法时,会抛出一个BlndException异常,获取message的自定义信息并返回 验证 UserEntity类 @Data @Entity @T ...
- 【spring boot】spring boot的自定义banner修改+spring boot启动项目图标修改
1.启动Spring Boot项目后会看到这样的图案,这个图片其实是可以自定义的,打开网站 http://patorjk.com/software/taag/#p=display&h=3&am ...
- layui 在页面弹出小窗口,并关闭
function showdialog() { layer.open({ type: 2, title: '发起调度', shadeClose: true, shade: 0.8, area: [ ...
- EIP Core2.0开源
EIP Core2 权限管理系统 (交流群:495070603,作者:1039318332) 开源地址: https://gitee.com/sunzewei/eipcore2 https://git ...
- Python 绘图与可视化 matplotlib 动态条形图 bar
bar的参考链接:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.bar.html 第一种办法 一种方法是每次都重新画,包括清除f ...
- 一、NodeJS入门——准备工作(1)——NodeJS的安装
目录 1.介绍 2.nodejs下载 3.nodejs安装 4.nodejs的简单实用 5.nodejs的经典入门:hello world 6.总结 1 介绍 这是一系列的内容主要是关于我在学习 ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!
原文地址 迁移到:http://www.bdata-cap.com/newsinfo/1741515.html 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和f ...
- VirtualBox安装Ubuntu-18.04-Server笔记
准备 安装'Windows Terminal' 安装WSL 安装VirtualBox 安装 虚拟磁盘映像文件选择创建在SSD(如果有) 选择openssh,公钥从GitHub获取,前提是GitHub已 ...
- 双栈(Dual Stack)
参考博客: 双栈数据结构: https://blog.csdn.net/hebtu666/article/details/83011115 https://blog.csdn.net/cainv89/ ...