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. 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.
参考了一个很清晰的DFS的code:
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
} class Solution{
public void cleanRoom(Robot robot) {
HashSet<String> visited = new HashSet<>();
helper(robot, 0, 0, visited);
} public void helper(Robot robot, int x, int y, HashSet<String> visited){
//store visted or block coordinate as string in visited set
String key = x + ":" + y;
if(visited.contains(key)){
return;
}
robot.clean;
visited.add(key); if(moveUp(robot)){
helper(robot, x-1, y, visited);
moveDown(robot);
}
if(moveDown(robot)){
helper(robot, x+1, y, visited);
moveUp(robot);
}
if(moveLeft(robot)){
helper(robot, x, y-1, visited);
moveRight(robot);
}
if(moveRight(robot)){
helper(robot, x, y+1, visited);
moveLeft(robot);
}
} public boolean moveUp(Robot robot){
return robot.move();
}
public boolean moveDown(Robot robot){
robot.turnLeft();
robot.turnLeft();
boolean res = robot.move();
robot.turnRight();
robot.turnRight();
return res; }
public boolean moveLeft(Robot robot){
robot.turnLeft();
boolean res = robot.move();
robot.turnRight();
return res;
}
public boolean moveRight(Robot robot){
robot.turnRight();
boolean res = robot.move();
robot.turnLeft();
return res;
} } class Robot {
boolean move() {
/*Default method by moving one step on the current direction
* will return true if move successfully*/
return true;
}
void turnLeft() {
/*change direction to +90*/
} void turnRight() {
/*change direction to -90*/
}
void clean() {
/*Do clean*/
}
}
LeetCode - Robot Room Cleaner的更多相关文章
- [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 ...
- 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 489. Robot Room Cleaner
原题链接在这里:https://leetcode.com/problems/robot-room-cleaner/ 题目: Given a robot cleaner in a room modele ...
- [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 922D】Robot Vacuum Cleaner
[链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就 ...
- CF922D Robot Vacuum Cleaner 贪心+排序
正确的贪心方法:按照比例排序. code: #include <bits/stdc++.h> #define N 200000 #define ll long long #define s ...
随机推荐
- Django框架教程之正则表达式URL误区详解
问题:我学习的视频大概是2015年录的,里面用的Django版本比较老关于正则表达式URL这一块都是用的url(“url(r'^admin/', admin.site.urls),”)方法.而我自己练 ...
- GC 是什么? 为什么要有GC?
C/C++中由程序员进行对象的回收像学校食堂中由学生收盘子,.Net 中由GC 进行垃圾回收像餐馆中店员 去回收. GC 是垃圾收集器(Garbage Collection).程序员不用担心内存管理, ...
- while循环与 for循环
import turtle turtle.setup(600,400,0,0) turtle.bgcolor('red') turtle.color('yellow') turtle.fillcolo ...
- 深入java----java内存区域及对象的创建
看完深入理解jvm之后自己再用图的方式进行一遍梳理,用以加深理解. 第一部分,首先对整体java运行时内存区域有一个整体框架式的了解. 运行时内存区域的划分如上图所示,那么接下里看看一个对象的创建又怎 ...
- font-family 中文字体列表
华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 儷黑 Pro:LiHei Pro ...
- Beta阶段冲刺二
Beta冲刺二 1.团队TSP 团队任务 预估时间 实际时间 完成日期 对数据库的最终完善 120 150 12.2 对学生注册功能的完善--新增触发器 150 140 11.29 对教师注册功能的完 ...
- 最近学习了Sqlite3数据库,写一下操作应用以及命令
首先使用Flask-SQLAlchemy管理数据库 使用pip安装:pip install flask-sqlalchemy 接着要配置数据库,定义模型 关于数据库的操作就不再写了.... 使用Fla ...
- Moya https配置方法
准备 iOS做https适配时对服务器是有一定要求的,服务端必须要是一个符合ATS(App Transport Security)要求的HTTPS.简单说要满足以下几个要求: 1.Transpor ...
- 将 Range 对象赋给变量
有多种方法将现有的 Range 对象赋给变量.本主题对两种不同的方法进行了阐述.在下列示例中,将 Range 对象赋给变量 Range1 和 Range2.例如,下列指令将活动文档中的第一个和第二个单 ...
- mysql 实现 sqlserver的row_number over() 方法
1.创建表 CREATE TABLE `heyf_t10` ( `empid` int(11) DEFAULT NULL, `deptid` int(11) DEFAULT NULL, `salary ...