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 ...
随机推荐
- Scrapy中间件user-agent和ip代理使用
一.定义实现随机User-Agent的下载中间件 1.在middlewares.py中完善代码 import random from Tencent.settings import USER_AGEN ...
- 七月在线爬虫班学习笔记(六)——scrapy爬虫整体示例
第六课主要内容: 爬豆瓣文本例程 douban 图片例程 douban_imgs 1.爬豆瓣文本例程 douban 目录结构 douban --douban --spiders --__init__. ...
- Java容器解析系列(7) ArrayDeque 详解
ArrayDeque,从名字上就可以看出来,其是通过数组实现的双端队列,我们先来看其源码: /** 有自动扩容机制; 不是线程安全的; 不允许添加null; 作为栈使用时比java.util.Stac ...
- IDEA新建项目时,没有Spring Initializr选项
换了台新电脑,然后重新安装了Intellij IDEA,创建spring boot项目的时候找不到Spring Initializr选项了. 然后百度了下,发现有前辈做出了回答,就复制存到了自己随笔里 ...
- jsp视频如何播放
网站开发小白们对如何插入视频有较大的困扰,一段时间不知道从何下手,想在数据库里面直接导入,但没能成功,后又尝试直接在myeclipse里面直接放入视频. 对于不同的播放器,视频的格式也有要求,建议使用 ...
- hive表分区操作
1.修复表分区命令 msck repair table table_name; 2.添加表分区操作 alter table table_name add partition(month_id='201 ...
- eclipse 项目中嵌入jetty
Jetty是一个提供HHTP服务器.HTTP客户端和javax.servlet容器的开源项目,Jetty 目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的 ...
- vgcreate语法
vgcreate 用于创建LVM卷组 补充说明 vgcreate命令 用于创建LVM卷组.卷组(Volume Group)将多个物理卷组织成一个整体,屏蔽了底层物理卷细节.在卷组上创建逻辑卷时不用考虑 ...
- QT自定义控件系列(二) --- Loading加载动画控件
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- Python 多线程的程序不结束多进程的程序不结束的区别
import time from threading import Thread from multiprocessing import Process #守护进程:主进程代码执行运行结束,守护进程随 ...