-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathProblem2.java
More file actions
68 lines (59 loc) · 2.43 KB
/
Problem2.java
File metadata and controls
68 lines (59 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Time Complexity : O(m*n)
// Space Complexity :O(m*n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : no
// Your code here along with comments explaining your approach
//Using BFS traversal we start by adding the start cell in the queue
//we then traverse up, down right and left of the maze until we find a wall and add them into the queue
//To prevent the visit of same cell again we mark the cells as visited whenever its being added to the queue
//If the destination is present inside the queue we return true
import java.util.*;
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
int m = maze.length;
int n = maze[0].length;
int [][] dirs = new int[][] {{-1,0}, {1,0}, {0,-1}, {0,1}};
if(start[0] == destination[0] && start[1] == destination[1]) return true;
Queue<int []> q = new LinkedList<>();
q.add(start);//add the start indices into the queue (0.4)
maze[start[0]][start[1]] = 2; //mark the start indices as visited ie 0,4 is visited
while(!q.isEmpty()) {
int[] cur = q.poll();
for(int[] dir: dirs) {
int r = cur[0];
int c = cur[1];
//the next 0 to go in the queue is not the immediate neighbour but the one above the obstacle or wall
while(r < m && r >=0 && c>=0 && c< n && maze[r][c] != 1) {
r += dir[0];
c += dir[1];
}
r -= dir[0];
c -= dir[1];
//check if r and c are the destination
if(r== destination[0] && c == destination[1]) return true;
//only if the neighbours are not marked visited add them to queue
if(maze[r][c] !=2) {
maze[r][c]= 2;
q.add(new int[]{r,c});
}
}
}
return false;
}
}
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
int[][] maze = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0}
};
int[] start = {0, 4};
int[] destination = {4, 4};
boolean result = sol.hasPath(maze, start, destination);
System.out.println(result);
}
}