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

[其他] google 扫地机器人

 
全局:

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

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

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

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

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



评分

参与人数 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;. 1point 3acres

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

  12.         public:

  13.                 bool move(int direction){
  14.                         int x = direction - d;
  15.                         if(x > 0){
  16.                                 turnR(x);. 1point3acres.com
  17.                         }else if(x < 0){
  18.                                 turnL(-x);-baidu 1point3acres
  19.                         }
  20.                         int ret = move();. 1point 3acres
  21.                         if(ret)
  22.                                 cout << "move:" << direction << endl;
  23.                         return ret;
  24.                 }
  25. .1point3acres
  26.                 bool move(){
  27.                         int dx = 0;
  28.                         int dy = 0;
  29.                         switch(d){
  30.                                 case 0:
  31.                                         dx --;
  32.                                         break;
  33.                                 case 1:
  34.                                         dy ++;. ----
  35.                                         break;. Χ
  36.                                 case 2:
  37.                                         dx ++;
  38.                                         break;. Waral dи,
  39.                                 case 3:.--
  40.                                         dy --;
  41.                                         break;
  42.                                 default:
    . 1point3acres
  43.                                         break;
  44.                         }
  45.                         int tx = x + dx;
  46.                         int ty = y + dy;
  47.                         if( tx >= N || tx < 0 || ty >= N || ty < 0){
  48.                                 return false;
  49.                         }
  50.                         if(data[tx][ty]) return false;
  51.                         x = tx;
  52.                         y = ty;. check 1point3acres for more.
  53.                         return true;-baidu 1point3acres
  54.                 }

  55.                 void turnL(int k){
  56.                         d -= k;
  57.                         if(d < 0){
  58.                                 d = -d;
  59.                                 d = d % 4;
  60.                                 d = 4 - d;
  61.                         }
  62.                 }

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


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

  83.                 void cleanAll(){
  84.                         unordered_set<int> vis;
  85.                         help(vis);
  86.                 }

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

  106. };. 1point3acres

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

评分

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

查看全部评分

回复

使用道具 举报

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

    private boolean move(){}

    private void turnLeft(){}

    private void turnRight(){}

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

    private boolean moveL(){
        turnLeft(1);
        boolean succ = move();
        if(succ)
            pos[0]--;
        turnRight(1);
        return succ;
    }

    private boolean moveR(){
        turnRight(1);-baidu 1point3acres
        boolean succ = move();
        if(succ)
            pos[0]++;
        turnLeft(1);. .и
        return succ;
    }

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

    private boolean moveNext(int x){
        if(x == 0) return moveF();.google  и
        else if(x == 1) return moveR();
        else if(x == 2) return moveB();
        else if(x == 3) return moveL();
        return false;
    }

    private void Return(int x){
        if(x == 0) moveB(); ..
        else if(x == 1) moveL();. Χ
        else if(x == 2) moveF();. 1point3acres
        else if(x == 3) moveR();
    }
. check 1point3acres for more.

    private void clean(){}

    private void traverse(){
        while(!stack.isEmpty()){
            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])){. 1point3acres
                    Return(actions.pop());
                }
            }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标记有没有访问过. 1point 3acres

最重要的是考虑好cornercase 怎么从现在状态回到上一部状态 什么变了 什么没变 除了坐标还要考虑方向
. check 1point3acres for more.
其实你自己写一个差不多就知道要考虑什么了
回复

使用道具 举报

🔗
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
诶 真的吗
不记录path还可以 就改成recursion就好啦
那部记录visited的话 难道是允许无效率的遍历吗? ...

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

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

使用道具 举报

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

评分

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

查看全部评分

回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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