原题链接在这里: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. [BAT脚本] 1、BAT脚本FOR循环操作文件和命令返回实例

    Wednesday, 31. October 2018 08:18PM - beautifulzzzz 一.需求 需要在windows上实现一个bat脚本解析json,将json转换为自己想要的key ...

  2. java核心技术(第十版卷一)笔记(纯干货!)

    这是我读过的第三本关于java基础的书.第一本<<java从入门到精通>>这本书让我灵识初开.第二本<<java敏捷开发>>这本书则是有一次被一位师傅批 ...

  3. redis在项目中的应用

    redis在项目中的应用  ps:PHP 会自动 关redis连接 不需要手动关 对于临时的数据 可以不经过数据库直接redis上操作<pre>/*消息队列实例 消息队列详细步骤在http ...

  4. python解决自动化测试静态页面加载慢的情况

    # coding:utf8from selenium import webdriverimport time # 创建一个ChromeOptions的对象option = webdriver.Chro ...

  5. c语言中static 函数和普通函数的区别

    C程序一直由下列部分组成: 1)正文段——CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 2)初始化数据段(数据段)——在程序中所有赋了初值的全局变量,存放 ...

  6. Hbase flusher源码解析(flush全代码流程解析)

    版权声明:本文为博主原创文章,遵循版权协议,转载请附上原文出处链接和本声明. 在介绍HBASE flush源码之前,我们先在逻辑上大体梳理一下,便于后续看代码.flush的整体流程分三个阶段 1.第一 ...

  7. AtomicReference示例

    对引用类型的原子性操作 /** * *对引用变量的原子操作 note: *在java中对引用类型的变量,赋值是原子性的,为什么还要有atomicReference,假如要对一个引用类型进行比较,设置等 ...

  8. freemarker模板文件的4个组成部分

    FreeMarker模板文件主要由以下4个部分组成:1.文本,直接输出的部分.2.注释,即<#–…–>格式不会输出.3.插值(Interpolation):即${..}或者#{..}格式的 ...

  9. 乘法器——booth编码

    博主最近在学习加法器.乘法器.IEEE的浮点数标准,作为数字IC的基础.当看到booth编码的乘法器时,对booth编码不是很理解,然后在网上找各种理解,终于豁然开朗.现将一个很好的解释分享给大家,希 ...

  10. SprintBoot 实现上传下载

    本人在 .NET 转JAVA 的路上 ,也在学习SpringBoot相关知识,这里记录一下在Springboot中实现文件上传下载的核心代码 package com.file.demo.springb ...