Advertisement
1988coder

The Maze I

Sep 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. /*
  2. BFS Solution
  3.  
  4. Time Complexity:
  5. O(V + E)
  6. V = number of vertices traveresed = m*n
  7. E = number of edges * time to find each edge = m*n * max(m,n)
  8. TC = O(mn + mn*max(m,n)) = O(mn*max(m,n))
  9.  
  10. Space Complexity = O(m*n) - visited array + queue can take m*n space in worst case.
  11. */
  12. class Solution {
  13.     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
  14.         if (maze == null || maze.length == 0 || maze[0].length == 0) {
  15.             return false;
  16.         }
  17.         if (start == null || destination == null || start.length != 2 || destination.length != 2) {
  18.             return false;
  19.         }
  20.         if (maze[start[0]][start[1]] == 1 || maze[destination[0]][destination[1]] == 1) {
  21.             return false;
  22.         }
  23.         if (start[0] == destination[0] && start[1] == destination[1]) {
  24.             return true;
  25.         }
  26.        
  27.         int rowNum = maze.length;
  28.         int colNum = maze[0].length;
  29.        
  30.         boolean[][] visited = new boolean[rowNum][colNum];
  31.         int[][] directions = new int[][] {{-1,0},{1,0},{0,-1},{0,1}};
  32.         Queue<int[]> queue = new LinkedList<>();
  33.        
  34.         queue.offer(start);
  35.         visited[start[0]][start[1]] = true;
  36.        
  37.         while (!queue.isEmpty()) {
  38.             int[] point = queue.poll();
  39.             for (int[] dir : directions) {
  40.                 int x = point[0];
  41.                 int y = point[1];
  42.                 while (x >= 0 && x < rowNum && y >= 0 && y < colNum && maze[x][y] == 0) {
  43.                     x += dir[0];
  44.                     y += dir[1];
  45.                 }
  46.                
  47.                 // Back to valid point;
  48.                 x -= dir[0];
  49.                 y -= dir[1];
  50.                 if (visited[x][y] == true) {
  51.                     continue;
  52.                 }
  53.                 visited[x][y] = true;
  54.                 if (x == destination[0] && y == destination[1]) {
  55.                     return true;
  56.                 }
  57.                 queue.offer(new int[]{x,y});
  58.             }
  59.         }
  60.         return false;
  61.     }
  62. }
  63.  
  64. /*
  65. DFS Solution
  66.  
  67. Time Complexity:
  68. O(V + E)
  69. V = number of vertices traveresed = m*n
  70. E = number of edges * time to find each edge = m*n * max(m,n)
  71. TC = O(mn + mn*max(m,n)) = O(mn*max(m,n))
  72.  
  73. Space Complexity = O(m*n) - visited array + queue can take m*n space in worst case.
  74. */
  75. // class Solution {
  76. //     public boolean hasPath(int[][] maze, int[] start, int[] destination) {
  77. //         if (maze == null || maze.length == 0 || maze[0].length == 0) {
  78. //             return false;
  79. //         }
  80. //         if (start == null || destination == null || start.length != 2 || destination.length != 2) {
  81. //             return false;
  82. //         }
  83. //         if (maze[start[0]][start[1]] == 1 || maze[destination[0]][destination[1]] == 1) {
  84. //             return false;
  85. //         }
  86. //         if (start[0] == destination[0] && start[1] == destination[1]) {
  87. //             return true;
  88. //         }
  89.        
  90. //         boolean[][] visited = new boolean[maze.length][maze[0].length];
  91. //         int[][] directions = new int[][] {{-1,0},{1,0},{0,-1},{0,1}};
  92.        
  93. //         return dfsHelper(maze, start, destination, visited, directions);
  94. //     }
  95.    
  96. //     public boolean dfsHelper(int[][] maze, int[] start, int[] destination, boolean[][] visited, int[][] directions) {
  97. //         if (visited[start[0]][start[1]] == true) {
  98. //             return false;
  99. //         }
  100. //         if (start[0] == destination[0] && start[1] == destination[1]) {
  101. //             return true;
  102. //         }
  103.        
  104. //         visited[start[0]][start[1]] = true;
  105.        
  106. //         for (int[] dir : directions) {
  107. //             int x = start[0];
  108. //             int y = start[1];
  109. //             while (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] == 0) {
  110. //                 x += dir[0];
  111. //                 y += dir[1];
  112. //             }
  113. //             // Back to valid point
  114. //             x -= dir[0];
  115. //             y -= dir[1];
  116. //             if (dfsHelper(maze, new int[] {x,y}, destination, visited, directions)) {
  117. //                 return true;
  118. //             }
  119. //         }
  120. //         return false;
  121. //     }
  122. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement