Guest User

Untitled

a guest
Oct 5th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import edu.princeton.cs.algs4.UF;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. public class MazeGenerator {
  9. Pair[] points;
  10. int x = 0;
  11. int rows;
  12. int columns;
  13. UF map;
  14. Random rand = new Random();
  15.  
  16. MazeGenerator(int m, int n) {
  17. if (m < 1 || n < 1)
  18. throw new ArithmeticException();
  19.  
  20. points = new Pair[m * n];
  21. rows = m;
  22. columns = n;
  23.  
  24. //Note: each index in UF is to be mapped to its Pair index counterpart.
  25. //Example: points[1] == find[1]
  26. map = new UF (m*n);
  27.  
  28. //Populate points array with Pair objects
  29. for (int j = 0; j < m; j++) {
  30. for (int i = 0; i < n; i++) {
  31. points[x++] = new Pair(i, j);
  32. }
  33. }
  34. }
  35.  
  36. public void findPath() {
  37. int current;
  38. int neighbor;
  39. List<Pair> unusedPairs = new ArrayList(Arrays.asList(points));
  40.  
  41. while(map.count() > 1) {
  42. current = rand.nextInt(unusedPairs.size());
  43. neighbor = findAdjacent(unusedPairs.get(current));
  44.  
  45. if (map.find(encode(unusedPairs.get(current))) == map.find(neighbor)) {
  46. System.out.println(map.find(encode(unusedPairs.get(current))));
  47. System.out.println(map.find(neighbor));
  48. System.out.println("Connected in same component already!");
  49. }
  50. else {
  51. map.union(encode(unusedPairs.get(current)), neighbor);
  52. System.out.println(unusedPairs.get(current) + ", " + points[neighbor]);
  53. unusedPairs.remove(unusedPairs.get(current));
  54. }
  55. }
  56. System.out.println("Maze is complete");
  57. }
  58.  
  59. public class Pair {
  60. Pair(int x, int y) {
  61. this.x = x;
  62. this.y = y;
  63. }
  64. //For testing purposes only
  65. @Override
  66. public String toString() {
  67. return ("(" + x + ", " + y + ")");
  68. }
  69.  
  70. final int x;
  71. final int y;
  72. }
  73.  
  74. /**
  75. //Theory used: Manhattan distance
  76. //Note: I don't have a use for this method yet, it was a suggestion.
  77. public boolean isAdjacent(Pair p, Pair q) {
  78. int distance = Math.abs(p.x - q.x) + Math.abs(q.y - q.y);
  79.  
  80. if (distance == 1) {
  81. return true;
  82. }
  83. return false;
  84. }*/
  85.  
  86. //An adjacent cell must be orthogonal.
  87. //Test for case: if edge, if side, else
  88. public int findAdjacent(Pair p) {
  89. final Pair n = new Pair(0,1);
  90. final Pair w = new Pair(1,0);
  91. final Pair s = new Pair(0,-1);
  92. final Pair e = new Pair(-1,0);
  93. final Pair[] nwse = {n, w, s, e};
  94. ArrayList<Pair> neighbors = new ArrayList<>();
  95.  
  96. //For every possible 4 directions of the cell, determine which directions are valid and store in arraylist.
  97. for(Pair direction : nwse) {
  98. if((0 <= p.x+direction.x) && (p.x+direction.x < columns) && (0 <= p.y+direction.y) && (p.y+direction.y < rows)) {
  99. neighbors.add(direction);
  100. }
  101. }
  102. //Generate random index based on size of arraylist
  103. int nbr = (int)(Math.random()* (neighbors.size()));
  104. Pair direction = neighbors.get(nbr);
  105. Pair neighbor = new Pair (direction.x + p.x, direction.y + p.y);
  106.  
  107. return encode(neighbor);
  108. }
  109.  
  110. //Change Pair to int representation for Union Find struct
  111. public int encode(Pair a) {
  112. return Math.abs(a.x) + Math.abs(a.y) + Math.abs((columns-1) * a.y);
  113. }
  114.  
  115. public static void main(String[] args) {
  116. MazeGenerator test = new MazeGenerator(4, 5);
  117. test.findPath();
  118.  
  119.  
  120. //Expected output: print out a line for each union connected.
  121. //Optional/EC: output a visual representation of maze
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment