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

狗家阳谷挂经 攒人品

🔗
bdhmwzfa 2019-3-3 13:01:31 | 只看该作者
全局:
tianchez 发表于 2019-3-3 11:48
我当时答得是,每次忘queue里面push的时候,不一定四个方向都一起push,每poll一次,可以随机选1-4个方向 ...

如果每次不把所有扩展出来的点放到队列里的话,你怎么保证所有的节点都可达呢?感觉这个比较tricky啊
回复

使用道具 举报

🔗
 楼主| tianchez 2019-3-3 13:55:21 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
 楼主| tianchez 2019-3-3 13:56:09 | 只看该作者
全局:
cjhsu0319 发表于 2019-3-3 12:35
请问在这种随选选取的机制下 bfs如何保证一定会到达指定得终点?

看我楼上的回答吧...
回复

使用道具 举报

🔗
visa 2019-3-3 14:05:57 | 只看该作者
全局:
tianchez 发表于 2019-3-3 13:55
嗯 刚才花了时间重新写了一下code,确实不能两层随机,我当时也只是最后时候口头说improve方法时说的的, ...

我今天一下午搞bfs,感觉得有个stack keep track, 索性我就用dfs了,感觉还是挺省事儿的
回复

使用道具 举报

🔗
visa 2019-3-3 14:06:59 | 只看该作者
全局:
楼主,我觉得你挺有实力的,差点运气,预祝楼主拿下好的包裹,哥们儿今年也是挺倒霉的,呵呵。。。
回复

使用道具 举报

🔗
 楼主| tianchez 2019-3-3 14:22:28 | 只看该作者
全局:
顺便po一下我刚才码的迷宫生成的bfs 和 dfs两个解好了... 再多攒点人品好了
  1. import java.util.*;

  2. public class Maze{

  3.     int[] start, end;
  4.     Cell[][] maze;
  5.     boolean[][] visited;
  6.     int[][] dirs = {{-1,0}, {1, 0}, {0, -1}, {0, 1}};



  7.     class Cell{
  8.         boolean up_open;
  9.         boolean down_open;
  10.         boolean left_open;
  11.         boolean right_open;
  12.     }

  13.     public Maze(int height, int width, int[] start, int[] end ){
  14.         maze = new Cell[height][width];
  15.         visited = new boolean[height][width];

  16.         for (int i=0; i<maze.length; i++)
  17.             for (int j=0; j<maze[0].length; j++){
  18.                 maze[i][j] = new Cell();
  19.             }
  20.         
  21.         this.start = start;
  22.         this.end = end;
  23.     }

  24.     public void bfs_construct(){
  25.         Queue<int[]> q = new LinkedList();
  26.         q.offer(start);
  27.         visited[start[0]][start[1]] = true;

  28.         while (!q.isEmpty()){
  29.             int[] pos = q.poll();
  30.             ArrayList<Integer> dir_set = getRandomDir();

  31.             for (int i: dir_set){
  32.                 int new_x = pos[0] + dirs[i][0];
  33.                 int new_y = pos[1] + dirs[i][1];
  34.                 if (inBound(new_x,new_y) && !visited[new_x][new_y]){
  35.                     Cell curr_cell = maze[pos[0]][pos[1]];
  36.                     Cell next_cell = maze[new_x][new_y];
  37.                     if (i == 0){
  38.                         curr_cell.up_open = true;
  39.                         next_cell.down_open = true;
  40.                     }
  41.                     else if (i == 1){
  42.                         curr_cell.down_open = true;
  43.                         next_cell.up_open = true;
  44.                     }
  45.                     else if (i == 2){
  46.                         curr_cell.left_open = true;
  47.                         next_cell.right_open = true;
  48.                     }
  49.                     else if (i == 3){
  50.                         curr_cell.right_open = true;
  51.                         next_cell.left_open = true;
  52.                     }
  53.                     q.offer(new int[]{new_x, new_y});
  54.                     visited[new_x][new_y] = true;
  55.                 }
  56.             }
  57.         }
  58.     }

  59.     public void dfs_construct(int x, int y){
  60.         if (!inBound(x, y)) return;
  61.         visited[x][y] = true;
  62.                 // pick random neighbor
  63.                 ArrayList<Integer> dir_set = getRandomDir();
  64.                 for (int i: dir_set){
  65.                     int new_x = x + dirs[i][0];
  66.                     int new_y = y + dirs[i][1];
  67.                     if (inBound(new_x,new_y) && !visited[new_x][new_y]){
  68.                         Cell curr_cell = maze[x][y];
  69.                         Cell next_cell = maze[new_x][new_y];
  70.                         if (i == 0){
  71.                             curr_cell.up_open = true;
  72.                             next_cell.down_open = true;
  73.                         }
  74.                         else if (i == 1){
  75.                             curr_cell.down_open = true;
  76.                             next_cell.up_open = true;
  77.                         }
  78.                         else if (i == 2){
  79.                             curr_cell.left_open = true;
  80.                             next_cell.right_open = true;
  81.                         }
  82.                         else if (i == 3){
  83.                             curr_cell.right_open = true;
  84.                             next_cell.left_open = true;
  85.                         }
  86.                         dfs_construct(new_x, new_y);
  87.                     }
  88.                 }
  89.     }

  90.     Random rand = new Random();
  91.     private ArrayList<Integer> getRandomDir(){
  92.         ArrayList<Integer> dir_set = new ArrayList();
  93.         while (dir_set.size()< 4){
  94.             int d = rand.nextInt(4);
  95.             if (!dir_set.contains(d)) dir_set.add(d);
  96.         }
  97.         return dir_set;
  98.         
  99.     }

  100.     private boolean inBound(int i, int j){
  101.         if (i < 0 || j < 0 || i > maze.length-1 || j > maze[0].length-1) return false;
  102.         return true;
  103.     }

  104.     public void print(){
  105.         for (int i=0; i<maze.length; i++){
  106.             String s1 = "";
  107.             String s2 = "";
  108.             String s3 = "";
  109.             for (int j=0; j<maze[0].length; j++){
  110.                 Cell curr_cell = maze[i][j];
  111.                 s1 += " ";
  112.                 if (curr_cell.up_open) s1 += " "; else s1 += "-";
  113.                 s1 += " ";
  114.                
  115.                 if (curr_cell.left_open) s2 += " "; else s2 += "|";
  116.                 if (start[0] == i && start[1] == j)
  117.                     s2 += "S";
  118.                 else  if (end[0] == i && end[1] == j)
  119.                     s2 += "E";
  120.                 else
  121.                     s2 += " ";
  122.                 if (curr_cell.right_open) s2 += " "; else s2 += "|";

  123.                 s3 += " ";
  124.                 if (curr_cell.down_open) s3 += " "; else s3 += "-";
  125.                 s3 += " ";
  126.             }
  127.             System.out.println(s1);
  128.             System.out.println(s2);
  129.             System.out.println(s3);
  130.         }
  131.     }
  132.     public static void main(String[] args){
  133.         Maze m = new Maze(10, 10, new int[]{1,3}, new int[]{8,7});
  134.         // m.print();
  135.         // m.bfs_construct();
  136.         m.dfs_construct(1,3);
  137.         m.print();
  138.         System.out.println();
  139.     }
  140. }
复制代码
回复

使用道具 举报

🔗
 楼主| tianchez 2019-3-3 14:26:21 | 只看该作者
全局:
visa 发表于 2019-3-3 14:06
楼主,我觉得你挺有实力的,差点运气,预祝楼主拿下好的包裹,哥们儿今年也是挺倒霉的,呵呵。。。

诶..都是看命的 随缘吧...我顺便把我刚才码的两个方法都po了代码..
多攒点人品吧...
回复

使用道具 举报

🔗
xuantong 2019-3-3 19:02:49 | 只看该作者
全局:
3, morri travesal?
回复

使用道具 举报

🔗
dlcnwhl 2019-3-4 04:20:49 | 只看该作者
全局:
第三题面试官除了只让用parent O(1)找下一个leaf节点,同时leaf的string也不允许拼接,必须online进行比较么?
回复

使用道具 举报

🔗
 楼主| tianchez 2019-3-4 04:46:58 | 只看该作者
全局:
dlcnwhl 发表于 2019-3-4 04:20
第三题面试官除了只让用parent O(1)找下一个leaf节点,同时leaf的string也不允许拼接,必须online进行比较 ...

什么叫online比较?
回复

使用道具 举报

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

本版积分规则

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