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 ...
随机推荐
- javascript20150124
表达式与运算符 表达式 与数学中的定义相似,表达式是指具有一定的值的.用运算符把常数和变量连接起来的代数式.一个表达式可以只包含一个常数或一个变量.运算符可以是四则运算符.关系运算符.位运算符.逻辑运 ...
- Android Stdio 无法打开模拟器
安装好了各种版本的AVD,有个版本4.1,API版本16,219MB的模拟器是可以打开的,但是基本不能用,只能看到首界面,跳转什么的完全不行. 除此之外其它高版本的模拟器都不能用(API版本>2 ...
- 前端校验框架ValidForm之check方法修正
用过validform的朋友相信都知道,在利用check方法的时候,发现该方法对表单输入值只要符合datatype规则的就返回ture.那么我们想对某个字段进行ajax重复校验的时候,只需要在该表单元 ...
- OO第一单元作业总结之初识面向对象
第一个单元的三次作业均为求导,循序渐进的让我们掌握如何构造类和方法,让整个代码是面向对象的设计而不是面向过程的设计.如果第一次作业和第二次作业你只是简单的对过程着手架构类,到了第三次作业就会变得格外麻 ...
- Sql Server 2012 集群配置
基于Windows Server 2008 R2的WSFC实现SQL Server 2012高可用性组(AlwaysOn Group) 2012年5月 微软新一代数据库产品SQL Server 201 ...
- 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 20165326
Exp5 MSF基础应用 实践内容 主动攻击实践 ms17_010_enternalblue 靶机:win7 x64成功 针对浏览器的攻击 ms14_064_ole_code_execution 靶机 ...
- 《A Knowledge-Grounded Neural Conversation Model》
abstract 现在的大多数模型都可以被应用在闲聊场景下,但是还没有证据表明他们可以应用在更有用的对话场景下.这篇论文提出了一个知识驱动的,带有背景知识的神经网络对话系统,目的是为了在对话中产生更有 ...
- tomcat管理监控工具:probe(可代替Tomcat Manager)
版本信息:tomcat8减压版.probe 3.0.0 修改tomcat用户配置,在conf\tomcat-users.xml加入一下配置: <role rolename="admin ...
- classPath与PATH
PATH是window的变量,而不是Java的变量: 通常配置PATH路径是为了找到需要的XX.exe命令,而且配置在用户的变量下面: 例如:JDK中的javac与java命令在cmd中使用,需要把命 ...
- 18-09-06天津 关于Excel的一些操作
1 字符串分后后一个返回值是个list 2个以上就是字符串 a = '/sldj/fj/'b ,c = a.strip('/').split('/')print(b,c) # sldj fj 2关于o ...