楼主: rhwfyf
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] google 扫地机器人

 
🔗
sth830 2018-4-7 09:20:54 | 只看该作者
全局:
siy 发表于 2018-3-9 09:24
我面试面了这道题最后过了。我是dfs,用一个set记录visited。设定起点是原点,dfs helper函数要传机器人当 ...

请问大神,follow up是啥?什么思路呢?
回复

使用道具 举报

🔗
TsengJuiWang 2018-4-7 09:23:34 | 只看该作者
全局:
flyPacific111 发表于 2018-4-5 06:40
you need a moveBack function in the end? I posted one but not sure it's correct. You can take a lo ...
  1. for(int i=0; i<directions.length; i++) {
  2.          TurnLeft(i);
  3.          if(Move()) {. From 1point 3acres bbs
  4.              DFS(myRobot,x + directions[i][0], y + directions[i][1], visited);
复制代码
像这样对吗?
回复

使用道具 举报

🔗
TsengJuiWang 2018-4-7 09:24:34 | 只看该作者
全局:

代码不见了,哭。。。。就是在move成功之后要TurnLeft(2), Move(), TurnLeft(2)对吗?
回复

使用道具 举报

🔗
lf963 2018-4-7 10:54:04 | 只看该作者
全局:
TsengJuiWang 发表于 2018-4-7 09:13
为什么move成功之后,调用完DFS没有调用方法把机器人放回到调用前的位置?

假設在當前的DFS層(假設這層DFS的名稱為Lv=1),機器人位在座標 x=2, y=3 的位置
並且 for loop 現在是 i=1
假設機器人可以往 i=1 的方向前進,也就是說 if(Move()) 是 true
則會調用 DFS(myRobot, x+0, y-1, visited) 進入下一層 DFS (假設這層DFS的名稱為Lv=2). From 1point 3acres bbs
當Lv=2層的DFS結束後,回到 Lv=1 層的DFS
這時機器人的座標仍然在 x=2, y=3的位置,因為要從Lv=1進入Lv=2之前,我並沒有修改x與y的值
這樣機器人仍然維持在Lv=1層的位置
不知道這樣有沒有問題
麻煩再互相討論一下
謝謝
回复

使用道具 举报

🔗
lf963 2018-4-7 11:06:07 | 只看该作者
全局:
flyPacific111 发表于 2018-4-5 06:40
you need a moveBack function in the end? I posted one but not sure it's correct. You can take a lo ...

我有看過你的方法,但不懂為甚麼要連續
turnLeft(0)
turnLeft(1).google  и
turnLeft(2)
turnLeft(3)

turnLeft(k)的意思是會向左轉k次
所以不應該是
turnLeft(1)
turnLeft(1)
turnLeft(1)
turnLeft(1)
嗎?

另外,關於DFS結束後的moveBack

假設在當前的DFS層(假設這層DFS的名稱為Lv=1),機器人位在座標 x=2, y=3 的位置-baidu 1point3acres
並且 for loop 現在是 i=1
假設機器人可以往 i=1 的方向前進,也就是說 if(Move()) 是 true
則會調用 DFS(myRobot, x+0, y-1, visited) 進入下一層 DFS (假設這層DFS的名稱為Lv=2)
當Lv=2層的DFS結束後,回到 Lv=1 層的DFS
這時機器人的座標仍然在 x=2, y=3的位置,因為要從Lv=1進入Lv=2之前,我並沒有修改x與y的值
這樣機器人仍然維持在Lv=1層的位置
不知道這樣有沒有問題
麻煩再互相討論一下
謝謝
回复

使用道具 举报

🔗
msu_HIDDEN 2018-4-7 11:45:20 | 只看该作者
全局:
这题有不用memorization 的解法吗?就是不记录走过的路径的那种
回复

使用道具 举报

🔗
TsengJuiWang 2018-4-7 12:41:54 | 只看该作者
全局:
lf963 发表于 2018-4-7 10:54
假設在當前的DFS層(假設這層DFS的名稱為Lv=1),機器人位在座標 x=2, y=3 的位置
並且 for loop 現在是 i ...

题里面说move成功的话机器人就移到下一个位置了,所以说虽然你没有改记得x和y但是机器人已经跑了,是这样吗,还是我理解错了
回复

使用道具 举报

🔗
TsengJuiWang 2018-4-7 12:42:47 | 只看该作者
全局:
lf963 发表于 2018-4-7 11:06
我有看過你的方法,但不懂為甚麼要連續
turnLeft(0)
turnLeft(1)

我记得turnLeft(k)是90*K
回复

使用道具 举报

🔗
lf963 2018-4-7 14:21:29 | 只看该作者
全局:
TsengJuiWang 发表于 2018-4-7 12:41.google  и
题里面说move成功的话机器人就移到下一个位置了,所以说虽然你没有改记得x和y但是机器人已经跑了,是这样 ...

你是正確的!是我搞錯了。下面是我的想法
  1. public static void main(String[] args){. ----
  2.         Robot myRobot = new Robot();
  3.        
  4.                                                                                 //3 means face toward north. 1point3acres
  5.         new RobotCleaner().DFS(myRobot, 0, 0, new HashSet<Coordinate>,3);   
  6. }
  7.     //faceTo: the position that the robot is now facing toward ..
  8.     //faceTo = 0: face toward west
  9.     //factTo = 1: face toward south
  10.     //faceTo = 2: face toward east
  11.     //factTo = 3: face toward north
  12.        
  13. void DFS(Robot myRobot, int x, int y, HashSet<Coordinate> visited, int faceTo){
  14.         if(visited.contains(new Coordinate(x,y)))
  15.                 return;
  16.         myRobot.Clean();
  17.         visited.add(new Coordinate(x, y));
  18.         int[][] directions = {{-1,0},{0,-1},{1,0},{0,1}};   //west south east north

  19.         //During each for loop iteration, the robot will turn left 90 degree. 1point3acres
  20.         //so after the for loop finish, the robot will face toward its original direction
  21.         for(int i=0; i<directions.length; i++){
  22.                 //faceTo is always between 0 to 3 inclusive
  23.                 faceTo = (faceTo + 1) % directions.length;
  24.                
  25.                 TurnLeft(1);    //turn left 90 degree
  26.                
  27.                 if(Move())
  28.                         DFS(myRobot,x + directions[faceTo][0], y + directions[faceTo][1], visited, faceTo);
  29.                
  30.                 else    //if next cell is obstacle or wall, add the next cell to visited.--
  31.                         visited.add(new Coordinate(x + directions[faceTo][0], y + directions[faceTo][1]));
  32.                 moveBack();
  33.         }
  34. }. .и

  35. void moveBack(){
  36.         TurnLeft(2);
  37.         Move();. 1point3acres.com
  38.         TurnLeft(2);
  39. }
  40.        
  41. class Coordinate{
  42.     int x;
  43.     int y;
  44.     Coordinate(int x, int y){
  45.         this.x = x;
  46.         this.y = y;.1point3acres
  47. }
复制代码

评分

参与人数 2大米 +8 收起 理由
高渐离击筑高歌 + 5 给你点个赞!
asdf61 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表