Advertisement
Guest User

maze kruskal algorithm

a guest
Jul 19th, 2010
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  1. int sizex = 100;
  2. int sizey = 100;
  3. MazeGenKruskal gen;
  4.  
  5. void setup()
  6. {
  7.   size(700, 700, P2D);
  8.   noLoop();
  9.   gen = new MazeGenKruskal();
  10. }
  11.  
  12. void draw()
  13. {
  14.   drawMaze(gen.createMaze(sizex, sizey));
  15. }
  16.  
  17. void keyPressed()
  18. {
  19.   redraw();
  20. }
  21.  
  22. void drawMaze(Cell[][] maze)
  23. {
  24.   background(0xFFAAAAAA);
  25.   float cellwidth = (float) width / sizex;
  26.   float cellheight = (float) height / sizey;
  27.  
  28.   for (int x = 0; x < sizex; x++)
  29.   {
  30.     for (int y = 0; y < sizey; y++)
  31.     {
  32.       pushMatrix();
  33.       translate(cellwidth * x, cellheight * y);
  34.       maze[x][y].draw(cellwidth, cellheight);
  35.       popMatrix();
  36.     }
  37.   }
  38. }
  39.  
  40. class Cell
  41. {
  42.  
  43.   Wall[] walls;
  44.   Cell next;
  45.   MazeGenKruskal.EquivalenceClass equivalenceClass;
  46.   boolean solution;
  47.   int type;
  48.  
  49.   Cell()
  50.   {
  51.     walls = new Wall[4];
  52.     solution = true;
  53.     type = 0;
  54.     next = null;
  55.   }
  56.  
  57.   void draw(float width, float height)
  58.   {
  59.     noStroke();
  60.     fill(type == 1 ? 0xFF00FF00 : type == 2 ? 0xFFFF0000 : solution ? 0xFF007777 : 0xFFAAAAAA);
  61.     rect(0, 0, width, height);
  62.     stroke(0);
  63.     strokeWeight(1);
  64.     if (walls[0].standing)
  65.     {
  66.       line(0, 0, width, 0);
  67.     }
  68.     if (walls[1].standing)
  69.     {
  70.       line(width, 0, width, height);
  71.     }
  72.     if (walls[2].standing)
  73.     {
  74.       line(0, height, width, height);
  75.     }
  76.     if (walls[3].standing)
  77.     {
  78.       line(0, 0, 0, height);
  79.     }
  80.     point(0,0);
  81.     point(0,width);
  82.     point(width,height);
  83.     point(0,height);
  84.   }
  85. }
  86.  
  87. class Wall
  88. {
  89.  
  90.   boolean standing;
  91.   Cell c1, c2;
  92.  
  93.   Wall(Cell c1, Cell c2)
  94.   {
  95.     standing = true;
  96.     this.c1 = c1;
  97.     this.c2 = c2;
  98.   }
  99. }
  100.  
  101. class MazeGenKruskal
  102. {
  103.  
  104.   Cell[][] maze = null;
  105.   int sizex = 0, sizey = 0;
  106.   Wall[] walls;
  107.   int index;
  108.   int count;
  109.  
  110.   Cell[][] createMaze(int x, int y)
  111.   {
  112.     maze = new Cell[x][y];
  113.     sizex = x;
  114.     sizey = y;
  115.     walls = new Wall[((sizex - 1) * sizey) + ((sizey - 1) * sizex)];
  116.     index = 0;
  117.     count = sizex * sizey;
  118.     createEmptyMaze();
  119.     createEquivalenceClasses();
  120.     process();
  121.     solveMaze();
  122.     return maze;
  123.   }
  124.  
  125.   void solveMaze()
  126.   {
  127.     for (int x = 0; x < sizex; x++)
  128.     {
  129.       for (int y = 0; y < sizey; y++)
  130.       {
  131.         if (maze[x][y].solution)
  132.         {
  133.           Cell c = maze[x][y];
  134.           int exits;
  135.           do
  136.           {
  137.             if (c.type > 0)
  138.             {
  139.               break;
  140.             }
  141.             exits = 0;
  142.             Cell co = null;
  143.             for (int i = 0; i < 4; i++)
  144.             {
  145.               Wall w = c.walls[i];
  146.               if (w.standing)
  147.               {
  148.                 continue;
  149.               }
  150.               Cell tempc = (c == w.c1) ? w.c2 : w.c1;
  151.               if (tempc.solution)
  152.               {
  153.                 exits++;
  154.                 co = tempc;
  155.               }
  156.             }
  157.             if (exits == 1)
  158.             {
  159.               c.solution = false;
  160.               c = co;
  161.             }
  162.           }
  163.           while (exits == 1);
  164.         }
  165.       }
  166.     }
  167.   }
  168.  
  169.   void process()
  170.   {
  171.     while (count > 1)
  172.     {
  173.       Wall w = walls[index];
  174.       index++;
  175.       if (w.c1.equivalenceClass != w.c2.equivalenceClass)
  176.       {
  177.         w.standing = false;
  178.         union(w.c1.equivalenceClass, w.c2.equivalenceClass);
  179.       }
  180.     }
  181.   }
  182.  
  183.   void createEquivalenceClasses()
  184.   {
  185.     for (int x = 0; x < sizex; x++)
  186.     {
  187.       for (int y = 0; y < sizey; y++)
  188.       {
  189.         new EquivalenceClass(maze[x][y]);
  190.       }
  191.     }
  192.   }
  193.  
  194.   void createEmptyMaze()
  195.   {
  196.     for (int x = 0; x < sizex; x++)
  197.     {
  198.       for (int y = 0; y < sizey; y++)
  199.       {
  200.         maze[x][y] = new Cell();
  201.       }
  202.     }
  203.  
  204.     for (int y = 0; y < sizey; y++)
  205.     {
  206.       maze[0][y].walls[3] = new Wall(null, null);
  207.       maze[sizex - 1][y].walls[1] = new Wall(null, null);
  208.     }
  209.  
  210.     for (int x = 0; x < sizex; x++)
  211.     {
  212.       maze[x][0].walls[0] = new Wall(null, null);
  213.       maze[x][sizey - 1].walls[2] = new Wall(null, null);
  214.     }
  215.  
  216.     for (int x = 0; x < sizex - 1; x++)
  217.     {
  218.       for (int y = 0; y < sizey; y++)
  219.       {
  220.         Wall w = new Wall(maze[x][y], maze[x + 1][y]);
  221.         maze[x][y].walls[1] = w;
  222.         maze[x + 1][y].walls[3] = w;
  223.         walls[index++] = w;
  224.       }
  225.     }
  226.  
  227.     for (int y = 0; y < sizey - 1; y++)
  228.     {
  229.       for (int x = 0; x < sizex; x++)
  230.       {
  231.         Wall w = new Wall(maze[x][y], maze[x][y + 1]);
  232.         maze[x][y].walls[2] = w;
  233.         maze[x][y + 1].walls[0] = w;
  234.         walls[index++] = w;
  235.       }
  236.     }
  237.  
  238.     index = 0;
  239.     maze[0][0].type = 1;
  240.     maze[sizex - 1][sizey - 1].type = 2;
  241.  
  242.     Random r = new Random();
  243.     for (int i = 0; i < walls.length; i++)
  244.     {
  245.       int pos = r.nextInt(walls.length - i) + i;
  246.       Wall temp = walls[i];
  247.       walls[i] = walls[pos];
  248.       walls[pos] = temp;
  249.     }
  250.   }
  251.  
  252.   void union(EquivalenceClass l1, EquivalenceClass l2)
  253.   {
  254.     if (l1.count < l2.count)
  255.       l2.joinClass(l1);
  256.     else
  257.       l1.joinClass(l2);
  258.     count--;
  259.   }
  260.  
  261.   class EquivalenceClass
  262.   {
  263.  
  264.     int count;
  265.     Cell first = null;
  266.     Cell last = null;
  267.  
  268.     EquivalenceClass(Cell c)
  269.     {
  270.       first = c;
  271.       last = c;
  272.       count = 1;
  273.       c.equivalenceClass = this;
  274.     }
  275.  
  276.     void joinClass(EquivalenceClass l)
  277.     {
  278.       Cell c = l.first;
  279.       do
  280.       {
  281.         c.equivalenceClass = this;
  282.       }
  283.       while ((c = c.next) != null);
  284.       l.last.next = first;
  285.       first = l.first;
  286.       count += l.count;
  287.     }
  288.   }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement