diff --git a/TheMaze.java b/TheMaze.java new file mode 100644 index 0000000..6f3614d --- /dev/null +++ b/TheMaze.java @@ -0,0 +1,45 @@ +// Time Complexity : O(m*n) +// Space Complexity : O(m*n) +// Did this code successfully run on Leetcode : Yes +// Approach : We perfrom BFS and explore the path of not crossing boundaries and not hitting the wall. We maintain a queue and add the start position to the queue first. Then explore all +// four directions from that point and check if we crossed the boundaries or hit the wall. If we hit the wall, we step back, add to queue if not previously visited and mark it visited. +// When we stop, we check if it is destination index and return true else continue to explore until queue is empty, that means processing of all indexes is done. + +class Solution { + int[][] dirs; + int m,n; + + public boolean hasPath(int[][] maze, int[] start, int[] destination) { + this.dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1}}; //all four directions array + this.m = maze.length; + this.n = maze[0].length; + + Queue q = new LinkedList<>();//using queue to perform BFS + q.add(new int[]{start[0], start[1]});//add the start point to the queue to start processing + maze[start[0]][start[1]] = 2; //mark it as visited by updating with 2 + + while(!q.isEmpty()){ + int[] curr = q.poll(); //process current row-column index + for(int[] dir: dirs){ //explore all four directions + int r = dir[0] + curr[0]; + int c = dir[1] + curr[1]; + + while(r>=0 && c>=0 && r