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 ...
随机推荐
- Android 偏门xml属性
在 recycleView listview scroview 等等 活动的时候会出现蓝边 android:overScrollMode="never" 用次属性可以去掉 fadi ...
- MySQL【日期和时间处理函数】的使用方法
名称 调用示例 示例结果 描述 NOW NOW() 2018-09-19 09:24:10 返回当前日期和时间 CURDATE CURDATE() 2018-09-19 返回当前日期 CURTIME ...
- Java冒泡法和二分法
最近去一家公司面试,手贱在人家CTO面前自告奋勇写了一把冒泡法,结果在交换数据的时候出了洋相,回家反思,写下如下代码,对自己算是一个鞭策,得到的教训是不要眼高手低,低调前行. package com. ...
- python 游戏 —— 汉诺塔(Hanoita)
python 游戏 —— 汉诺塔(Hanoita) 一.汉诺塔问题 1. 问题来源 问题源于印度的一个古老传说,大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆 ...
- 前端特效demo | 值得收藏的6个 HTML5 Canvas 实用案例
HTML5 动画在Canvas 上得到了充分的发挥,我们 VIP 视频也分享过很多相关的动画特效制作视频,这次给大家带来 6 款超炫酷的HTML5 canvas 动画的 demo,一起来看看吧~ 文内 ...
- webSocket的 原理 及 实现
websocket协议是基于Tcp的一种新的网络协议,它实现了客户端与服务器的双向通行,并允许服务端主动发送信息给客户端.WebSocket是html5中的协议. Http协议与WebSocket协议 ...
- jar包是干什么用的
1.JAR(Java ARchive)是将一系列文件到单个压缩文件里,就象Zip那样.但是,同Java中其他任何东西一样,JAR文件是跨平台的,所以不必关心涉及具体平台的问题.2.jar包就是别人已经 ...
- 第三次Scrum编程冲刺
小组第三次冲刺的任务及其完成情况描述 回顾第二次任务,完成了基本业务:用户可以使拍摄视频并上传至个人的空间页面,来与关注他的粉丝分享.关注“长大故事”社区中的其他用户,来了解别人的动态.别人所发动态下 ...
- git教程:工作区和暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- leftBarButtonItem 的颜色
修改系统:leftBarButtonItem, rightBarButtonItem 的颜色 在你需要修改的页面,ViewDidload()方法里面贴上下面代码 self.navigationCont ...