cs61b homework9

第一遍看作业要求没看懂。。不过看懂了后算法实现起来还是蛮简单的,主要在于遍历每个墙时,找出其分隔的两个元素,若两元素间无path,把墙拆了2333,有path的话墙不拆,注意那个它给定union(root1,root2)函数接受的两个参数必须是root,所以正确的写法应该是union(find(i),find(j)),若传入了非root的元素会产生死循环。

代码:

 1 private class Walls{
 2       int h;
 3       int v;
 4       boolean isHoriz;
 5       Walls(int a,int b,boolean c){
 6           h=a;
 7           v=b;
 8           isHoriz=c;
 9       }
10   }
11   public Maze(int horizontalSize, int verticalSize) {
12     int i, j;
13 
14     horiz = horizontalSize;
15     vert = verticalSize;
16     if ((horiz < 1) || (vert < 1) || ((horiz == 1) && (vert == 1))) {
17       return;                                    // There are no interior walls
18     }
19 
20     // Create all of the horizontal interior walls.  Initially, every
21     // horizontal wall exists; they will be removed later by the maze
22     // generation algorithm.
23     if (vert > 1) {
24       hWalls = new boolean[horiz][vert - 1];
25       for (j = 0; j < vert - 1; j++) {
26         for (i = 0; i < horiz; i++) {
27           hWalls[i][j] = true;
28         }
29       }
30     }
31     // Create all of the vertical interior walls.
32     if (horiz > 1) {
33       vWalls = new boolean[horiz - 1][vert];
34       for (i = 0; i < horiz - 1; i++) {
35         for (j = 0; j < vert; j++) {
36           vWalls[i][j] = true;
37         }
38       }
39     }
40     int wallnumbers=horiz*(vert-1)+vert*(horiz-1);
41     Walls[]walls=new Walls[wallnumbers];
42     int count=0;
43     for( i=0;i<horiz;i++){
44         for( j=0;j<vert-1;j++){
45             walls[count]=new Walls(i,j,true);
46             count++;
47         }
48     }
49     for(i=0;i<horiz-1;i++){
50         for(j=0;j<vert;j++){
51             walls[count]=new Walls(i,j,false);
52               count++;           
53         }
54     }
55     for(i=wallnumbers-1;i>0;i--){
56         int r=randInt(i);
57         Walls template=walls[i];
58         walls[i]=walls[r];
59         walls[r]=template;
60     }
61     DisjointSets set=new DisjointSets(this.horiz*this.vert);
62     try{
63     for(i=0;i<wallnumbers;i++){
64         if(walls[i].isHoriz){
65             int number1=walls[i].h+walls[i].v*horiz;
66             int number2=walls[i].h+(walls[i].v+1)*horiz;
67             if(set.find(number1)==set.find(number2)){
68                 hWalls[walls[i].h][walls[i].v]=true;
69             }
70             else{
71                 hWalls[walls[i].h][walls[i].v]=false;
72                 set.union(set.find(number1),set.find(number2));
73             }
74         }
75         else if(!walls[i].isHoriz){
76             int number1=walls[i].h+walls[i].v*horiz;
77             int number2=number1+1;
78             if(set.find(number1)==set.find(number2))
79                 vWalls[walls[i].h][walls[i].v]=true;
80             else{
81                 vWalls[walls[i].h][walls[i].v]=false;
82                 set.union(set.find(number1), set.find(number2));
83             }
84         }
85     }
86     }catch(Exception e){
87         e.printStackTrace();
88     }
View Code

运行结果:

一个10*10的maze:

---------------------
| |   | |   | |     |
+ +-+ + + +-+ + +-+-+
| |     | |   | |   |
+ +-+ +-+ +-+ + + +-+
|       |     |     |
+-+ + +-+-+-+ +-+ +-+
|   |   |       |   |
+ +-+ + +-+ + + + +-+
| | | |   | | | |   |
+-+ +-+ +-+-+-+ + +-+
|   | |       |     |
+-+ + +-+ + +-+-+ + +
|   | | | | |     | |
+ + + + + + +-+-+ +-+
| | |     | | | |   |
+ +-+ +-+ +-+ + + +-+
|     | |       |   |
+-+ +-+ + +-+-+ + + +
|     |   |       | |
+--------------------
What a fine maze you've created!

part2 那个没太搞懂啊,如果用DFS的意思是不是每次搜到一层的话,随机选一个没有visited的edge,然后其他的edge全无效,这样保障两个vertice之间只有一条path?

posted @ 2017-08-02 09:05  想做码农的熊孩子  阅读(237)  评论(0编辑  收藏  举报