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 ...
随机推荐
- 如何用jmeter进行数据库性能测试
由于业务需要,需要进行数据库性能测试,记录过程进行学习 测试前期准备: 1.测试点准备及需求点 a.性能测试目的 b.jmeter测试数据库基本脚本 get c.数据库性能监控工具选择 d.服务器性 ...
- SQL语句:如何让字符串转化数字
和前端联调的时候,突然出现一个状况,新增数据的时候,一直报系统错误,写下此文,留以后反复温习.菜鸟程序员一名~ 项目内容:新增产品信息 具体实现:1 获取基础信息,创建产品(调用接口传入的产品类型,如 ...
- L1-061 新胖子公式
根据钱江晚报官方微博的报导,最新的肥胖计算方法为:体重(kg) / 身高(m) 的平方.如果超过 25,你就是胖子.于是本题就请你编写程序自动判断一个人到底算不算胖子. 输入格式: 输入在一行中给出两 ...
- Spring MVC流程
这是spring mvc框架结构图,图片是很早在网上撸过来的,具体在哪忘了…… 早期学习Springmvc 并没有具体了解过,只知道这样用很爽,最近了解下基本结构流程及组件所在…… 执行流程 Spri ...
- 算数运算符& 关系运算符
java中的算数运算符 + - * / % ++ -- + +: 加法运算符 - : 减法运算符 *: 乘法运算符 / : 除法运算符 注意: 除数不能为0,除数为0会报出异常 整数运算默认得到整数, ...
- 初读"Thinking in Java"读书笔记之第八章 --- 多态
再论向上转型 在某些方法中,仅接收基类作为参数,而不是特殊的导出类,会使得程序更容易扩展. 转机 方法调用绑定 前期绑定(编译期绑定):在编译期将一个方法调用和方法主体关联起来. 后期绑定(动态绑定或 ...
- IP通信基础课堂笔记----第一章(重点)
七层数据传输:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 1.物理层:设备----集线器.Hub 两台主机的最大跨度----2500m 在物理层工作----(1)所有设备都处于同一冲突 ...
- NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接
NHibernate数据库配置参数在hibernate.cfg.xml中 <?xml version="1.0" encoding="utf-8"?> ...
- WebStorm Goland PyCharm 等JetBrains验证服务器
http://idea.iteblog.com/key.php http://idea.idbry.com 2017年12月08日20:05:08 http://xdouble.cn:8888/ ht ...
- 北大poj- 1034
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3272 Accepted: 1313 Sp ...