JINGCHI.AI店面利口489扫地机器人

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();
}

DFS

  • 递归出口:如果扫过了就走。
  • 本状态处理:扫地,给memo加记录。
  • 递归,四方向:转身试挪动,递归,回溯到原位置原方向。

细节

  • 本题挪动时控制两个对象:一个是用机器人的API让它真的往四个方向试着挪动;一个是用传统的dxdy计算挪动坐标用于去重。
  • 坐标用set去重有小trick,可以以规范化的string形式放入set。
  • 本题就是回溯的时候因为API不方便,和一般的dfs写得不太一样而已,别的没什么大变化。

Reference: jasminemzy - leetcode489 - Robot Room Cleaner - hard

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 那个穿白衬衣微笑的男孩 时常出现在梦里 我以为我忘记了 其实我已经习惯了思念 我要有多爱你 才能不想你 二十年这份...
    挂居宝阅读 135评论 0 0
  • 本文参加#未完待续,就要表白#活动,本人承诺,文章为原创,且未在其他平台发表过。 又是一年杨絮纷飞、沙枣飘香的时节...
    石河子大学SHZU阅读 344评论 17 21
  • 王小波先生有两个小说,一个叫似水流年,一个叫三十而立,今天我借来用用。因为接上级通知(我奶奶),按照农历算,上周我...
    DinoF阅读 537评论 1 0