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

[其他] google 扫地机器人

 
🔗
liushaobo 2018-4-2 00:46:01 | 只看该作者
全局:
Ostrichi 发表于 2018-3-9 09:10
class robot{
   
    int[] pos;

想问下这个Return(actions.pop());
actions是应该存之前的move的方向么?
然后这行代码的意思是一直往回退直到退到上一格。那是不是只要退一格就好。。不需要while???. 1point 3acres
回复

使用道具 举报

全局:
rhwfyf 发表于 2018-3-20 13:14. Χ
要求就是把地都扫完。
move, turnLeft, turnRight, clean可以认为是提供的API,不需要自己实现。

我写了一个方法,楼主看一下是否有问题?
.google  и
public class Solution{
   
    public void turnLeft(int n);
    public void turnRight(int n);
    public boolean move();
    public void clean();
    ..
    public void cleanRoom().--
    {
        Set<int[]> visited = new HashSet<int[]>();. check 1point3acres for more.
        dfs(0, 0, visited, 0);
    }
    . 1point3acres
    private void dfs(int x, int y, Set<int[]> visited, int direction)
    {
        if(visited.contains(pos))
        {
            moveBack(direction);. 1point3acres
            return;
        }.google  и
        
        int[] pos = {x, y};
        visited.add(pos);
        clean();
        
        // start to clean 4 directions
        turnLeft(0);
        if(move())
        {
            dfs(x, y - 1, visited, 0);.1point3acres
        }
        else
        {
            visited.add(new int[]{x, y - 1});
        }
        
        turnLeft(1);
        if(move())
        {
            dfs(x, y + 1, visited, 1);
        }
        else
        {
            visited.add(new int[]{x, y + 1});
        }
        
        turnLeft(2);.1point3acres
        if(move()). ----
        {
            dfs(x + 1, y, visited, 2);
        }. check 1point3acres for more.
        else
        {
            visited.add(new int[]{x + 1, y});. 1point 3acres
        }
        
        turnLeft(3);
        if(move())
        {
            dfs(x, y - 1, visited, 3);
        }
        else. ----
        {. ----
            visited.add(new int[]{x, y - 1});
        }
        .--
        moveback(directoin);
    }
   
    // move back to previous cell and adjust direction
    private void moveBack(int n)
    {
        turnLeft(2);
        move();. 1point3acres.com
        turnRight(n);
    }
}

补充内容 (2018-4-4 11:45):
moveBack function 有个小bug,应该是下面这样:.google  и
turnLeft(2);
        move();
        turnLeft(2);
        turnRight(n);
回复

使用道具 举报

🔗
jobseekermk 2018-4-4 21:57:02 | 只看该作者
全局:

  1. offset = [[0, -1], [1, 0], [0, 1], [-1, 0]]

  2. def my_clean(robot, visited, x, y):
  3.     if (x, y) in visited:
  4.         return. ----
  5.     robot.clean()
  6.     visited.add((x, y))
  7.     for i in range(4):
  8.         robot.turnLeft()
  9.         if robot.move():. 1point 3acres
  10.             my_clean(robot, visited, x + offset[i][0], y + offset[i][1])
  11.             
  12.             # back to original point
  13.             robot.turnLeft()
  14.             robot.turnLeft(). ----
  15.             robot.move()
  16.         
  17.             # reset facing direction
  18.             robot.turnLeft()
  19.             robot.turnLeft()
复制代码
回复

使用道具 举报

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

請問是這樣嗎?
  1. void DFS(Robot myRobot, int x, int y, HashSet<Coordinate> visited){. ----
  2.         if(visited.contains(new Coordinate(x,y)))
  3.             return;
  4.         myRobot.Clean();
  5.         visited.add(new Coordinate(x, y));
  6.         int[][] directions = {{-1,0},{0,-1},{1,0},{0,1}};
  7.         for(int i=0; i<directions.length; i++){-baidu 1point3acres
  8.             TurnLeft(i);
  9.             if(Move())
  10.                 DFS(myRobot,x + directions[i][0], y + directions[i][1], visited);
  11.             else    //if next cell is obstacle or wall, add the next cell to visited
  12.                 visited.add(new Coordinate(x + directions[i][0], y + directions[i][1]));
  13.             TurnRight(i);
  14.         }
  15.     }

  16. class Robot{
  17.     // returns true if next cell is open and robot moves into the cell..1point3acres
  18.     // returns false if next cell is obstacle and robot stays on the current cell..
  19.     boolean Move(){}

  20.     // Robot will stay on the same cell after calling Turn*. k indicates how
  21.     // many turns to perform.
  22.     void TurnLeft(int k){}.1point3acres
  23.     void TurnRight(int k){}

  24.     // Clean the current cell.
  25.     void Clean(){}

  26.     boolean Move(Direction d){}
  27. }

  28. .1point3acres
  29. class Coordinate{
  30.     int x;. Waral dи,
  31.     int y;. Waral dи,
  32.     Coordinate(int x, int y){
  33.         this.x = x;
  34.         this.y = y;
  35.     }
  36. }
复制代码
回复

使用道具 举报

全局:
lf963 发表于 2018-4-5 05:45 ..
請問是這樣嗎?

you need a moveBack function in the end? I posted one but not sure it's correct. You can take a look.
回复

使用道具 举报

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

-baidu 1point3acres请问那个Followup 是啥呢?
回复

使用道具 举报

🔗
blactangeri 2018-4-6 15:03:35 | 只看该作者
全局:
flyPacific111 发表于 2018-4-4 10:52
我写了一个方法,楼主看一下是否有问题?. check 1point3acres for more.

public class Solution{

您好!
本帖隐藏的内容需要积分高于 195 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 195 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
blactangeri 2018-4-6 15:03:44 | 只看该作者
全局:
flyPacific111 发表于 2018-4-4 10:52
我写了一个方法,楼主看一下是否有问题?

public class Solution{

您好!
本帖隐藏的内容需要积分高于 195 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 195 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

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

你好,我在试着建模,把这题当 OO 做。
所以想问一下,调用 `move(direction)`,是指机器人会自动转向然后前进一步吗?
. Waral dи,
补充内容 (2018-4-6 21:46):
`dfs` 的 helper 里面换方向应该是可以直接 `move(direction)` 的对吧?还是必须 `turnxxx()` 再 `move()`?
回复

使用道具 举报

🔗
TsengJuiWang 2018-4-7 09:13:40 | 只看该作者
全局:
lf963 发表于 2018-4-5 05:45
請問是這樣嗎?

为什么move成功之后,调用完DFS没有调用方法把机器人放回到调用前的位置?
回复

使用道具 举报

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

本版积分规则

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