Advertisement
PAXSemperFidelis

Untitled

Nov 30th, 2021
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. ackage 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 - 2, width - 2);
  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, int maxRow, int maxCol) {
  57. // return true if row number and
  58. // column number is in range
  59. return (row >= 0) && (row < maxRow) &&
  60. (col >= 0) && (col < maxCol);
  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. if (mat[src.x][src.y] != 1 ||
  74. mat[dest.x][dest.y] != 1)
  75. return Integer.MAX_VALUE;
  76.  
  77. boolean[][] visited = new boolean[mat[0].length][mat.length];
  78.  
  79. // Mark the source cell as visited
  80. visited[src.x][src.y] = true;
  81.  
  82. // Create a queue for BFS
  83. Queue<queueNode> q = new LinkedList<>();
  84.  
  85. // Distance of source cell is 0
  86. queueNode s = new queueNode(src, 0);
  87. q.add(s); // Enqueue source cell
  88.  
  89. // Do a BFS starting from source cell
  90. while (!q.isEmpty()) {
  91. queueNode curr = q.peek();
  92. Point pt = curr.pt;
  93.  
  94. // If we have reached the destination cell,
  95. // we are done
  96. if (pt.x == dest.x && pt.y == dest.y)
  97. return curr.dist;
  98.  
  99. // Otherwise dequeue the front cell
  100. // in the queue and enqueue
  101. // its adjacent cells
  102. q.remove();
  103.  
  104. for (int i = 0; i < 4; i++) {
  105. int row = pt.x + rowNum[i];
  106. int col = pt.y + colNum[i];
  107.  
  108. // if adjacent cell is valid, has path
  109. // and not visited yet, enqueue it.
  110. if (isValid(row, col, mat[0].length, mat.length) &&
  111. mat[row][col] == 1 &&
  112. !visited[row][col]) {
  113. // mark cell as visited and enqueue it
  114. visited[row][col] = true;
  115. queueNode Adjcell = new queueNode
  116. (new Point(row, col),
  117. curr.dist + 1);
  118. q.add(Adjcell);
  119. }
  120. }
  121. }
  122.  
  123. // Return -1 if destination cannot be reached
  124. return Integer.MAX_VALUE;
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement