查看: 19214| 回复: 59
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] google 扫地机器人

 
全局:

2018(4-6月)-CS博士+fresh grad 无实习或全职 | 内推| 码农类General全职@google

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
看了地里对扫地机器人这道题的讨论,零零散散的还是没太搞明白。. 1point3acres
这道题挺高频的,地里大牛这么多,大家来讨论讨论这道题具体该怎么做?
谢谢!. From 1point 3acres bbs

Robot Clearner
Givena robot cleaner in a room modeled as a grid. Each cell in the grid can be emptyor blocked. The robot cleaner can move forward, turn left or turn right. Whenit tries to move into a blocked cell, its bumper sensor detects the obstacleand it stays on the current cell.
Thecontrol API:
interfaceRobot {
  //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.
  booleanMove();
  //Robot will stay on the same cell after calling Turn*. k indicates how
  //many turns to perform.
  voidTurnLeft(int k);
  voidTurnRight(int k);
  //Clean the current cell.
  voidClean();more.
  booleanMove(Direction d);
}
-baidu 1point3acres


评分

参与人数 2大米 +11 收起 理由
清水河醋鱼1995 + 1 很有用的信息!
blactangeri + 10 欢迎来一亩三分地论坛!

查看全部评分


上一篇:求问大家有没有什么交流技巧,把summer intern推到fall做?顺便求助选offer的问题
下一篇:amazon刚挂完又有HR联系
推荐
siy 2018-3-9 09:24:57 | 只看该作者
全局:
我面试面了这道题最后过了。我是dfs,用一个set记录visited。设定起点是原点,dfs helper函数要传机器人当前方向,用0123表示就可以。每一个helper函数用for循环走四次。每一次按照顺时针或者逆时针转一下。把走过的坐标和cannot move的点都放进visited。最重要的是走完发现无路可走之后要再转向后方走一步再转向后,也就是返回上级helper函数时机器人所在的位置和面朝的方向。

评分

参与人数 8大米 +36 收起 理由
yangruirui421 + 5 给你点个赞!
yabay91 + 5 很有用的信息!
greenroses + 3 给你点个赞!
abcdldzy + 4 给你点个赞!
jaychsu + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

推荐
wuwei321 2018-3-20 13:09:07 | 只看该作者
全局:
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_set>
  4. using namespace std;
  5. . ----
  6. class Robot{
  7.         private:
  8.                 vector<vector<int>> data;. .и
  9.                 int x;
  10.                 int y;
  11.                 int N;
  12.                 int d;//direction.1point3acres

  13. . 1point 3acres
  14.         public:

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

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

  56.                 void turnL(int k){
  57.                         d -= k;
  58.                         if(d < 0){.
  59.                                 d = -d;
  60.                                 d = d % 4;
  61.                                 d = 4 - d;
  62.                         }
  63.                 }
  64. . 1point3acres
  65.                 void turnR(int k){
  66.                         d += k;
  67.                         d = d % 4;
  68.                 }. Χ


  69.                 void clean(){
  70.                         cout << "clean:" << x << ":" << y << endl;.--
  71.                 }. 1point3acres
  72. . 1point3acres.com
  73.                 Robot(){
  74.                         N = 4;
  75.                         data = {
  76.                                 {0,0,0,1},
  77.                                 {0,1,0,0},. From 1point 3acres bbs
  78.                                 {0,0,1,1},
  79.                                 {0,0,0,0}
  80.                         };
  81.                         x = 0;
  82.                         y = 0;. .и
  83.                         d = 0;
  84.                 }

  85.                 void cleanAll(){
  86.                         unordered_set<int> vis;-baidu 1point3acres
  87.                         help(vis);
  88.                 }-baidu 1point3acres

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

  110. int main() {
  111.         Robot r;.--
  112.         r.cleanAll();
  113.         return 0;
  114. }
复制代码

评分

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

查看全部评分

回复

使用道具 举报

推荐
Ostrichi 2018-3-9 09:10:31 | 只看该作者
全局:
class robot{
   
. Χ    int[] pos;
.google  и    Set<String> visited;
    Stack<int[]> stack;
    Stack<Integer> actions;
-baidu 1point3acres
    robot(){
        pos = new int[]{0, 0};
        visited = new HashSet<>(); ..
        stack = new Stack<>();
. 1point 3 acres        stack.push(new int[]{0,0,0});
        actions = new Stack<>();
    }. ----

    private boolean move(){}

    private void turnLeft(){}

    private void turnRight(){}.google  и

    private boolean moveF(){
        boolean succ = move();
        if(succ)
            pos[1]++;
        return succ;
    }

    private boolean moveL(){
        turnLeft(1);. From 1point 3acres bbs
        boolean succ = move();
        if(succ)
            pos[0]--;
        turnRight(1);
        return succ;
    }. ----

    private boolean moveR(){
        turnRight(1);
        boolean succ = move();. check 1point3acres for more.
        if(succ)
            pos[0]++;
        turnLeft(1);
        return succ;
    }
. check 1point3acres for more.
    private boolean moveB(){
        turnLeft(2);
        boolean succ = move();. From 1point 3acres bbs
        if(succ)
            pos[1]--;
        turnRight(2);
        return succ;
    }
.1point3acres
    private boolean moveNext(int x){.
        if(x == 0) return moveF();
        else if(x == 1) return moveR();
        else if(x == 2) return moveB();
        else if(x == 3) return moveL();
        return false;
    }. From 1point 3acres bbs

    private void Return(int x){-baidu 1point3acres
        if(x == 0) moveB();
        else if(x == 1) moveL();
        else if(x == 2) moveF();
        else if(x == 3) moveR();
    }


    private void clean(){}
.--
    private void traverse(){
        while(!stack.isEmpty()){.1point3acres
            int[] cur = stack.peek();
            int x = cur[0], y = cur[1];
            
            if(!visited(posStr(cur[0], cur[1]))){
                visited.add(posStr(cur[0], cur[1]));
                clean(); . .и
            }

            String[] nextPoint = new String[]{posStr(cur[0], cur[1]+1),posStr(cur[0]+1, cur[1]),
                        posStr(cur[0], cur[1]-1),posStr(cur[0]-1, cur[1])};

            while(cur[2] < 4 && (visited.contains(nextPoint(cur[2])) || !moveNext(cur[2])))
                cur[2]++;
.--
            if(cur[2] == 4){
                stack.pop();
                while(!stack.isEmpty() && !(pos[0] == stack.peek()[0] && pos[1] == stack.peek()[1])){
                    Return(actions.pop());. ----
                }.google  и
            }else{
                cur[2]++;
                stack.push(new int[]{pos[0], pos[1], 0});
            }
        }
    }
}


. 1point3acres.com

评分

参与人数 4大米 +12 收起 理由
高渐离击筑高歌 + 5 给你点个赞!
liushaobo + 3 很有启发
rhwfyf + 3 给你点个赞!
bowenzh + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
sarahzjn 2018-3-9 05:32:45 | 只看该作者
全局:
同问。。。。这个题一直没搞清楚,感觉难点在于不知道房间的大小?然后用set记录位置尽量少走回头路?
回复

使用道具 举报

🔗
greynut 2018-3-9 05:33:56 | 只看该作者
全局:
我觉得应该用dfs 然后需要开一个stack记录path (或者recursion)

用一个visited标记有没有访问过

最重要的是考虑好cornercase 怎么从现在状态回到上一部状态 什么变了 什么没变 除了坐标还要考虑方向.--

其实你自己写一个差不多就知道要考虑什么了
回复

使用道具 举报

🔗
csehao 2018-3-9 06:45:49 | 只看该作者
全局:
就是DFS 唯一的区别就是要手动控制 把move和turn的action封装一下就可以了
回复

使用道具 举报

🔗
lyxuan1208 2018-3-9 08:03:29 | 只看该作者
全局:
然而面试官的意思是不用记录path不用记录visited
回复

使用道具 举报

🔗
greynut 2018-3-9 08:16:25 | 只看该作者
全局:
lyxuan1208 发表于 2018-3-9 08:03
然而面试官的意思是不用记录path不用记录visited

诶 真的吗
不记录path还可以 就改成recursion就好啦
那部记录visited的话 难道是允许无效率的遍历吗??

能不能提供下思路呢。。。 虽然知道了也没什么用了估计
回复

使用道具 举报

🔗
lyxuan1208 2018-3-9 08:31:46 | 只看该作者
全局:
greynut 发表于 2018-3-9 08:16.1point3acres
诶 真的吗 . 1point3acres.com
不记录path还可以 就改成recursion就好啦
那部记录visited的话 难道是允许无效率的遍历吗? ...

我同学去面挂了说的,我也不知道思路,不记录visited很容易就成环了,感觉不对。。。
所以假设可以用visited,是假设当前点为原点记录已经访问过的点?-baidu 1point3acres

补充内容 (2018-3-9 08:40):
也可能是canMove() 函数自动记录了visited
回复

使用道具 举报

🔗
cai_lw 2018-3-9 08:50:55 | 只看该作者
全局:
无论是判断是否存在不重复的路径,还是找最短路径,即使障碍物全部可见,也是NP-complete的
所以肯定是允许重复经过同一个格子的,也不会要求路径最短,自己随便制定一些看上去不太蠢的策略应该就可以了

评分

参与人数 1大米 +5 收起 理由
xh_pku + 5 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
kathywyq88 2018-3-9 08:57:41 | 只看该作者
全局:
面博士也考一样的题吗
回复

使用道具 举报

🔗
bagelbagel 2018-3-9 09:02:49 | 只看该作者
全局:
这题不是17年年初就有
回复

使用道具 举报

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

本版积分规则

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