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

[其他] google 扫地机器人

 
🔗
groundzyy1 2018-3-10 13:09:02 | 只看该作者
全局:
bagelbagel 发表于 2018-3-9 09:02
这题不是17年年初就有

这应该就是最早出现的时间,哈哈
回复

使用道具 举报

🔗
 楼主| rhwfyf 2018-3-10 13:28:00 | 只看该作者
全局:
siy 发表于 2018-3-10 02:48
差不多。但是api给的是顺时针转逆时针转。所以四个if里面应该是转0次1次2次3次。然后cannot move的点是房 ...

在dfs当前层,假如机器人朝向north,那么上左下右4个方向,在进入下一层递归之前,也要调整机器人方向,使其始终朝向north? 比如moveLeft,就是turn left(1), move one step, turn right(1),机器人还是朝向north?
回复

使用道具 举报

🔗
siy 2018-3-11 11:30:30 | 只看该作者
全局:
rhwfyf 发表于 2018-3-10 13:28. 1point3acres.com
在dfs当前层,假如机器人朝向north,那么上左下右4个方向,在进入下一层递归之前,也要调整机器人方向, ...

不需要。下一层helper时机器人的方向无所谓。但是无路可走时需要返回上一步的位置和方向。比如说现在在原点。现在执行这层helper的第(1/2/3/4)次loop。需要turn(0/1/2/3次)再move。现在在新点。从这个点再执行helper。发现无路可走就需要回到原点。因为你原点的helper要执行forloop四次。有可能没执行完全部四次。这就需要控制机器人回到原点并朝向刚来原点时候的方向。这样再执行下一个loop的时候turn的次数才和loop数相关。
回复

使用道具 举报

🔗
maniacalmm 2018-3-18 15:38:23 | 只看该作者
全局:
rhwfyf 发表于 2018-3-10 01:09
差不多是这个意思?. check 1point3acres for more.

void dfs(x, y, back_direction) {

如果是if(visited(x,y)), 那接下来执行的应该是move(back_direciton)再return吧,毕竟move如果true的话那机器人已经移动到那个位置了。
回复

使用道具 举报

🔗
nsf2000 2018-3-19 09:57:18 | 只看该作者
全局:
siy 发表于 2018-3-11 11:30
不需要。下一层helper时机器人的方向无所谓。但是无路可走时需要返回上一步的位置和方向。比如说现在在原 ...

请问为什么下一层helper的时候不需要确定方向呢……如果机器人面向东边,和机器人面向西边的记录visited方式应该不一样吧……
回复

使用道具 举报

🔗
l553585 2018-3-19 14:48:43 | 只看该作者
全局:
siy 发表于 2018-3-9 09:28
我面试的时候小哥说我是他面过的第一个把这道题题和followup都做完的candidate。难点在于一是包了api进去要 ...

请问参数化这个房间指什么
回复

使用道具 举报

🔗
l553585 2018-3-19 15:03:15 | 只看该作者
全局:
这题的意思不是很清楚,最后是要求什么,是求怎么走最短,还是只要把地都扫了就行了?
. 1point3acres
我们已经有的API是哪写啊?
turn //顺时针逆时针转
move(direction)
clean

turn 和 move 都需要我们再具体实现吗?
还有direction类是怎么表示的
回复

使用道具 举报

🔗
wuwei321 2018-3-20 13:09:07 | 只看该作者
全局:
  1. #include <iostream>.google  и
  2. #include <vector>
  3. #include <unordered_set>. check 1point3acres for more.
  4. using namespace std;. 1point3acres.com

  5. class Robot{
  6.         private:
  7.                 vector<vector<int>> data;
  8.                 int x;.1point3acres
  9.                 int y;
  10.                 int N;
  11.                 int d;//direction

  12.         public:. .и

  13.                 bool move(int direction){
  14.                         int x = direction - d;
  15.                         if(x > 0){
  16.                                 turnR(x);
  17.                         }else if(x < 0){
  18.                                 turnL(-x);
  19.                         }
  20.                         int ret = move();
  21.                         if(ret)
  22.                                 cout << "move:" << direction << endl;
  23.                         return ret;
  24.                 }

  25.                 bool move(){
  26.                         int dx = 0;.google  и
  27.                         int dy = 0;
  28.                         switch(d){
  29.                                 case 0:
  30.                                         dx --;
  31.                                         break;. From 1point 3acres bbs
  32.                                 case 1:
  33.                                         dy ++;
  34.                                         break;
  35.                                 case 2:
  36.                                         dx ++;
  37.                                         break;
  38.                                 case 3:
  39.                                         dy --;
  40.                                         break;. 1point3acres.com
  41.                                 default:
  42.                                         break;
  43.                         }
  44.                         int tx = x + dx;
  45.                         int ty = y + dy;
  46.                         if( tx >= N || tx < 0 || ty >= N || ty < 0){
  47.                                 return false;
  48.                         }
  49.                         if(data[tx][ty]) return false;
  50.                         x = tx;
  51.                         y = ty;
  52.                         return true;
  53.                 }
  54. .1point3acres
  55.                 void turnL(int k){
  56.                         d -= k;
  57.                         if(d < 0){
  58.                                 d = -d;
  59.                                 d = d % 4;. Waral dи,
  60.                                 d = 4 - d;
  61.                         }
  62.                 }

  63.                 void turnR(int k){. 1point3acres.com
  64.                         d += k;
  65.                         d = d % 4;
  66.                 }


  67.                 void clean(){
  68.                         cout << "clean:" << x << ":" << y << endl;
  69.                 }

  70.                 Robot(){.
  71.                         N = 4;
  72.                         data = {
  73.                                 {0,0,0,1},
  74.                                 {0,1,0,0},
  75.                                 {0,0,1,1},
  76.                                 {0,0,0,0}
  77.                         };
  78.                         x = 0;
  79.                         y = 0;
  80.                         d = 0;. Χ
  81.                 }

  82.                 void cleanAll(){
  83.                         unordered_set<int> vis;. check 1point3acres for more.
  84.                         help(vis);
  85.                 }

  86.                 void help(unordered_set<int> & vis){
  87.                         if(vis.find(x * N + y) != vis.end()) return;
  88.                         clean();
  89.                         vis.insert(x * N + y);
  90.                         int back[4] = {2, 3, 0, 1};
  91.                         int dx[4] = {-1,0, 1, 0};
  92.                         int dy[4] = {0,1, 0, -1};
  93.                         for(int i = 0; i < 4; i ++){
  94.                                 int tx = x + dx[i];
  95.                                 int ty = y + dy[i];
  96.                                 if( tx >= N || tx < 0 || ty >= N || ty < 0)continue;
  97.                                 if(vis.find(tx * N + ty) != vis.end()) continue;
  98.                                 bool ret = move(i);
  99.                                 if(ret) {
  100.                                         help(vis);
  101.                                         move(back[i]);
  102.                                 }
  103.                         }
  104.                 }
  105. . 1point 3acres
  106. };

  107. int main() {
  108.         Robot r;
    .--
  109.         r.cleanAll();. .и
  110.         return 0;
  111. }
复制代码

评分

参与人数 1大米 +3 收起 理由
一亩菠萝地 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| rhwfyf 2018-3-20 13:14:19 | 只看该作者
全局:
hululei 发表于 2018-3-19 15:03
这题的意思不是很清楚,最后是要求什么,是求怎么走最短,还是只要把地都扫了就行了?.

我们已经有的API ...

要求就是把地都扫完。
move, turnLeft, turnRight, clean可以认为是提供的API,不需要自己实现。
回复

使用道具 举报

🔗
l553585 2018-3-20 13:37:57 | 只看该作者
全局:
rhwfyf 发表于 2018-3-20 13:14
要求就是把地都扫完。
move, turnLeft, turnRight, clean可以认为是提供的API,不需要自己实现。

为啥人家的代码里move 和turn 都是要实现的
回复

使用道具 举报

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

本版积分规则

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