📣 独立日限时特惠: VIP通行证立减$68
楼主: 不zhiwei何
跳转到指定楼层
上一主题 下一主题
收起左侧

谷歌电面跪经

 
🔗
Urumic 2017-11-15 03:05:04 | 只看该作者
全局:
这个应该用DFS吧,因为更贴切机器人一次只走一条路的行为吧。
回复

使用道具 举报

🔗
8888888 2017-12-4 07:43:27 | 只看该作者
全局:
reliveinfire 发表于 2017-8-16 14:04
我的想法是dfs traversal, 当从dfs离开, 要回到之前的点, 并且将robot的方向, 找到call dfs时的方向, 不知 ...

请问为什么需要机器人最后回到dfs之前的点
回复

使用道具 举报

🔗
8888888 2017-12-4 08:00:09 | 只看该作者
全局:
baudelaire 发表于 2017-8-21 20:34
感觉是dfs+backtrack 不过房子太大了可能会爆栈。 写了一下代码 请大神们指教!

请问最后为什么 orient robot to oposite direction of d
回复

使用道具 举报

🔗
baudelaire 2017-12-7 21:11:35 | 只看该作者
全局:
8888888 发表于 2017-12-4 08:00
请问最后为什么 orient robot to oposite direction of d

其实就是backtrack到最初的状态 就像一般dfs一样
回复

使用道具 举报

🔗
YUANSHAO 2017-12-13 03:19:01 | 只看该作者
全局:
baudelaire 发表于 2017-8-21 20:34
感觉是dfs+backtrack 不过房子太大了可能会爆栈。 写了一下代码 请大神们指教!

会不会robot一个格子需要先走过去 再走回来的情况
XXOXX
XXOXX
OOSOO
XXOXX

S是出发点 需要先清扫上面 然后返回请扫下面 如果把访问过的点加入memo 是不是不会再访问这一个点呢

谢谢
回复

使用道具 举报

🔗
onerhao 2017-12-21 09:57:37 | 只看该作者
全局:
其实就是搜索,回溯法搜索房间的每一个点,这里要用dfs,因为机器人不能同时出现在两个地方,所以用不了bfs。
回复

使用道具 举报

全局:
这非常像一道AI中的search题目。

根据AI的思想, 首先根据提供的API定义出自己想要的actions(三个方向移动和返回原来的位置),然后利用这些action解决问题。

  1.     interface Robot {
  2.         Stack<String> actions = new Stack<>(); // store completed actions
  3.         Stack<String> plans = new Stack<>();    // store future actions
  4.         int[] curPosition = new int[2]; // mark the position in the robot's view
  5.         Set<String> visited = new HashSet<>(); // store the visited position
  6.         // actions

  7.         // returns true if next cell is open and robot moves into the cell.
  8.         // returns false if next cell is obstacle and robot stays on the current cell.
  9.         boolean Move();
  10.         // Robot will stay on the same cell after calling Turn*. k indicates hown many turns to perform.
  11.         void TurnLeft(int k);
  12.         void TurnRight(int k);
  13.         // Clean the current cell.
  14.         void Clean();
  15.         boolean Move(Direction d);

  16.         // keep the facing direction when moving left / right
  17.         // String[] actions = new String[]{"Forward", "Left", "Right"};

  18.         boolean MoveForward(){
  19.             // actions.push("Forward");

  20.             boolean success = Move();
  21.             if(success) ++curPosition[1];
  22.         }

  23.         boolean MoveLeft(){
  24.             // actions.push("Left");
  25.             
  26.             TurnLeft(1);
  27.             boolean success = Move();
  28.             if(success) --curPosition[0];
  29.             TurnRight(1);
  30.             return success;
  31.         }

  32.         boolean MoveRight(){
  33.             // actions.push("Right");
  34.             TurnRight(1);
  35.             boolean success = Move();
  36.             if(success) ++curPosition[0];
  37.             TurnLeft(1);
  38.             return success;
  39.         }


  40.         // explore new space
  41.         boolean Actuator(String direction){
  42.             if(direction.equals("Forward")){
  43.                 actions.push(direction);
  44.                 return MoveForward();
  45.             }
  46.             if(direction.equals("Left")) {
  47.                 actions.push(direction);
  48.                 return MoveLeft();
  49.             }
  50.             if(direction.equals("Right")){
  51.                 actions.push(direction);   
  52.                 return MoveRight();
  53.             }
  54.             return false;
  55.         }

  56.         // return to the previous position
  57.         boolean Return(){
  58.             String lastAction = stack.pop();
  59.             if(lastAction.equals("Left")) return MoveRight();
  60.             if(lastAction.equals("Right")) return MoveLeft();
  61.             if(lastAction.equals("Forward")) return MoveBackforward();
  62.         }


  63.         // main function
  64.         boolean Solver(Robot robot){
  65.             
  66.             String posStr = posStr = curPosition[0] + "," + curPosition[1];
  67.             visited.add(posStr);
  68.             Clean();
  69.             plans.push("Forward");
  70.             plans.push("Left");
  71.             plans.push("Right");
  72.             while(!plans.empty()){
  73.                 String plan = plans.pop();
  74.                 if(!Actuator(plan)) {
  75.                     actions.pop();
  76.                     continue;
  77.                 }
  78.                 posStr = curPosition[0] + "," + curPosition[1];
  79.                 if(!visited.contains(posStr)){
  80.                     Clean();
  81.                     visited.add(posStr);
  82.                     plans.push("Forward");
  83.                     plans.push("Left");
  84.                     plans.push("Right");
  85.                 }else{
  86.                     Return();
  87.                 }
  88.             }
  89.             return true;
  90.         }

  91.     }
复制代码
回复

使用道具 举报

🔗
freipan 2018-1-5 13:46:07 | 只看该作者
全局:
baudelaire 发表于 2017-8-21 20:34
感觉是dfs+backtrack 不过房子太大了可能会爆栈。 写了一下代码 请大神们指教!

对这段代码不理解,
                if(this.Move()){
                    dfsClean(nx, ny, nd, memo);// take care to orient to oposite direction of nd
                    // move back and turn to nd
                    this.Move();
                    // reset to direction before
                    this.TurnRight(2);
                }else{
是不是应该:
                if(this.Move()){
                    dfsClean(nx, ny, nd, memo);// take care to orient to oposite direction of nd
                    // move back and turn to nd
                    this.TurnRight(2);
                    this.Move();
                    // reset to direction before
                    this.TurnRight(2);
                }else{
回复

使用道具 举报

🔗
renou 2018-1-15 03:05:34 | 只看该作者
全局:
baudelaire 发表于 2017-8-21 20:34
感觉是dfs+backtrack 不过房子太大了可能会爆栈。 写了一下代码 请大神们指教!

谢谢lz的代码!我感觉LZ第17行这里应该也需要把ncode加入memo?否则机器人会回头吧?
回复

使用道具 举报

🔗
zqyzdsjdy 2018-1-16 01:21:33 | 只看该作者
全局:
一个考完托福的废人 发表于 2018-1-1 04:12
这非常像一道AI中的search题目。

根据AI的思想, 首先根据提供的API定义出自己想要的actions(三个方向 ...

感谢层主但我有个问题啊,我们不知道机器人最开始的朝向,比如是面向上方还是面向下方,这个时候你记录的curpos[2]的这个数组就完全反了啊
回复

使用道具 举报

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

本版积分规则

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