原题链接在这里:https://leetcode.com/problems/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:

  1. 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.
  2. The robot's initial position will always be in an accessible cell.
  3. The initial direction of the robot will be facing up.
  4. All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
  5. Assume all four edges of the grid are all surrounded by wall.

题解:

For the robot itself, it doesn't know the grid board. But it could treat its current location as (0, 0).

For current direction, if robot could move forward, continue dfs on next grid, then backtracking. Then change to next direction.

If it could not more forward, change to next direction.

Have a visited set to record visited coordnates. It the it has visited this coordnates, simply return.

Note: every time curD + 1, not curD + i.

Time Complexity: O(mn). m = grid.length. n = grid[0].length.

Space: O(m*n). stack space.

AC Java:

 /**
* // This is the robot's control interface.
* // You should not implement it, or speculate about its implementation
* interface Robot {
* // Returns true if the cell in front is open and robot moves into the cell.
* // Returns false if the cell in front is blocked and robot stays in the current cell.
* public boolean move();
*
* // Robot will stay in the same cell after calling turnLeft/turnRight.
* // Each turn will be 90 degrees.
* public void turnLeft();
* public void turnRight();
*
* // Clean the current cell.
* public void clean();
* }
*/
class Solution {
int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public void cleanRoom(Robot robot) {
HashSet<String> visited = new HashSet<>();
dfsClean(robot, 0, 0, 0, visited);
} private void dfsClean(Robot robot, int x, int y, int curD, Set<String> visited){
String coord = x + "," + y;
if(visited.contains(coord)){
return;
} visited.add(coord);
robot.clean();
for(int i = 0; i<4; i++){
if(robot.move()){
int dx = x + dirs[curD][0];
int dy = y + dirs[curD][1];
dfsClean(robot, dx, dy, curD, visited); // backtracking
robot.turnLeft();
robot.turnLeft();
robot.move();
robot.turnLeft();
robot.turnLeft();
} robot.turnRight();
curD = (curD + 1) % 4;
}
}
}

类似Walls and Gates.

LeetCode 489. Robot Room Cleaner的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. CodeForces - 922D Robot Vacuum Cleaner (贪心)

    Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...

  5. Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)

    题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...

  6. [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 ...

  7. 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 ...

  8. LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)

    题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...

  9. LeetCode 657. Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

随机推荐

  1. swagger Unable to render this definition

    Unable to render this definition The provided definition does not specify a valid version field. Ple ...

  2. 零次学习(Zero-Shot Learning)入门(转)

    很久没有更文章了,主要是没有找到zero-shot learning(ZSL)方面我特别想要分享的文章,且中间有一段时间在考虑要不要继续做这个题目,再加上我懒 (¬_¬),所以一直拖到了现在. 最近科 ...

  3. 【javascript】判断是否为正整数

    function isNormalInteger(str) { var n = Math.floor(Number(str)); return n !== Infinity && St ...

  4. 基于 HTML5 WebGL 的 3D 智慧隧道漫游巡检

    前言 这次为大家展示的是通过 HT for Web 灵活的图型化编辑工具打造的智慧隧道监控系统.通过 HTML5 技术实现了桌面和移动端的跨平台性,同时现实了可视化运维. 这次主要跟大家分享里面的漫游 ...

  5. 【学习笔记】薛定谔的喵咪Cat—球盒问题(全详解)

    [学习笔记]薛定谔的喵咪Cat-球盒问题(全详解) [题目描述] 当一个猫在盒子里时,因为放射物的状态我们不知道,所以猫的状态我们也不知道,这就所谓猫的生死纠缠态,也是所谓的薛定谔的猫. 当我们做需要 ...

  6. 2019-11-29-C#-性能分析-反射-VS-配置文件-VS-预编译

    原文:2019-11-29-C#-性能分析-反射-VS-配置文件-VS-预编译 title author date CreateTime categories C# 性能分析 反射 VS 配置文件 V ...

  7. ScheduledThreadPoolExecutor使用指南

    ScheduledThreadPoolExecutor是Timer的多线程实现版本,JDK官方推荐使用.ScheduledThreadPoolExecutor用于替代Timer.是接口Schedule ...

  8. Sqlserver表值函数来获取逗号分隔的ID

    其功能为: 将字符串如'1,2,3,4,5,6' 拼接成SQL里面的id 1:使用: select * from Student where id IN( SELECT * FROM dbo.F_SP ...

  9. javascript工厂函数(factory function)vs构造函数(constructor function)

    如果你从其他语言转到javascript语言的开发,你会发现有很多让你晕掉的术语,其中工厂函数(factory function)和构造函数(constructor function)就是其中的一个. ...

  10. 深入浅出JVM之垃圾收集算法

    判断哪些对象需要被回收 引用计数算法: 给对象中添加一个引用计数器,每当有一个地方引用时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 但是JVM没有使 ...