Advertisement
PAXSemperFidelis

Untitled

Nov 30th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. package maze;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.stream.Stream;
  6. import java.util.LinkedList;
  7. import java.util.Queue;
  8.  
  9. public class Maze {
  10. static class Point {
  11. int x;
  12. int y;
  13.  
  14. public Point(int x, int y) {
  15. this.x = x;
  16. this.y = y;
  17. }
  18. };
  19.  
  20. // A Data Structure for queue used in BFS
  21. static class queueNode {
  22. Point pt; // The coordinates of a cell
  23. int dist; // cell's distance of from the source
  24.  
  25. public queueNode(Point pt, int dist) {
  26. this.pt = pt;
  27. this.dist = dist;
  28. }
  29. };
  30.  
  31. public int shortestPath(char[][] maze) {
  32. int height = maze.length;
  33. int width = maze[0].length;
  34.  
  35. Point source = new Point(1, 1);
  36. Point dest = new Point(height - 1, width - 1);
  37.  
  38. int[][] maze2 = new int[height][width];
  39.  
  40. for (int i = 0; i < height; i++)
  41. {
  42. for (int j = 0; j < width; j++)
  43. {
  44. if(maze[i][j] == '.')
  45. maze2[i][j] = 1;
  46. else maze[i][j] = 0;
  47. }
  48. }
  49.  
  50. // Implement your path-finding algorithm here!
  51. return BFS(maze2, source, dest);
  52. }
  53.  
  54. // check whether given cell (row, col)
  55. // is a valid cell or not.
  56. static boolean isValid(int row, int col) {
  57. // return true if row number and
  58. // column number is in range
  59. return (row >= 0) && (row < ROW) &&
  60. (col >= 0) && (col < COL);
  61. }
  62.  
  63. // These arrays are used to get row and column
  64. // numbers of 4 neighbours of a given cell
  65. static int rowNum[] = {-1, 0, 0, 1};
  66. static int colNum[] = {0, -1, 1, 0};
  67.  
  68. // function to find the shortest path between
  69. // a given source cell to a destination cell.
  70. static int BFS(int mat[][], Point src,
  71. Point dest) {
  72. // check source and destination cell
  73. // of the matrix have value 1
  74. if (mat[src.x][src.y] != 1 ||
  75. mat[dest.x][dest.y] != 1)
  76. return -1;
  77.  
  78. boolean[][] visited = new boolean[ROW][COL];
  79.  
  80. // Mark the source cell as visited
  81. visited[src.x][src.y] = true;
  82.  
  83. // Create a queue for BFS
  84. Queue<queueNode> q = new LinkedList<>();
  85.  
  86. // Distance of source cell is 0
  87. queueNode s = new queueNode(src, 0);
  88. q.add(s); // Enqueue source cell
  89.  
  90. // Do a BFS starting from source cell
  91. while (!q.isEmpty()) {
  92. queueNode curr = q.peek();
  93. Point pt = curr.pt;
  94.  
  95. // If we have reached the destination cell,
  96. // we are done
  97. if (pt.x == dest.x && pt.y == dest.y)
  98. return curr.dist;
  99.  
  100. // Otherwise dequeue the front cell
  101. // in the queue and enqueue
  102. // its adjacent cells
  103. q.remove();
  104.  
  105. for (int i = 0; i < 4; i++) {
  106. int row = pt.x + rowNum[i];
  107. int col = pt.y + colNum[i];
  108.  
  109. // if adjacent cell is valid, has path
  110. // and not visited yet, enqueue it.
  111. if (isValid(row, col) &&
  112. mat[row][col] == 1 &&
  113. !visited[row][col]) {
  114. // mark cell as visited and enqueue it
  115. visited[row][col] = true;
  116. queueNode Adjcell = new queueNode
  117. (new Point(row, col),
  118. curr.dist + 1);
  119. q.add(Adjcell);
  120. }
  121. }
  122. }
  123.  
  124. // Return -1 if destination cannot be reached
  125. return Integer.MAX_VALUE;
  126. }
  127.  
  128. // Driver Code
  129. public static void main(String[] args) {
  130. int mat[][] = {{1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
  131. {1, 0, 1, 0, 1, 1, 1, 0, 1, 1},
  132. {1, 1, 1, 0, 1, 1, 0, 1, 0, 1},
  133. {0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
  134. {1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
  135. {1, 0, 1, 1, 1, 1, 0, 1, 0, 0},
  136. {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  137. {1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
  138. {1, 1, 0, 0, 0, 0, 1, 0, 0, 1}};
  139.  
  140. Point source = new Point(0, 0);
  141. Point dest = new Point(3, 4);
  142.  
  143. int dist = BFS(mat, source, dest);
  144.  
  145. if (dist != -1)
  146. System.out.println("Shortest Path is " + dist);
  147. else
  148. System.out.println("Shortest Path doesn't exist");
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement