From 78fb3dbdc885ae345cd9d2a3e5304e921ccd58b3 Mon Sep 17 00:00:00 2001 From: Dranez Date: Sat, 7 Feb 2026 19:19:19 -0500 Subject: [PATCH] Completed Graph1 --- TheMaze.java | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ TownJudge.java | 26 +++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 TheMaze.java create mode 100644 TownJudge.java diff --git a/TheMaze.java b/TheMaze.java new file mode 100644 index 0000000..59b4682 --- /dev/null +++ b/TheMaze.java @@ -0,0 +1,86 @@ +// 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 +// We traverse all 4 directions from a point and add the stop points to a queue +// Pop from the queue and mark as visited and repeat to find further stop points +// If we stop at the destination return true else false + +import java.util.Queue; + +class Solution { + int[][] dirs; + int m,n; + + public boolean hasPathDFS(int[][] maze, int[] start, int[] destination) { + this.dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; + m = maze.length; + n = maze[0].length; + return dfs(maze, start, destination); + } + + private boolean dfs(int[][] maze, int[] start, int[] destination){ + if(start[0] == destination[0] && start[1] == destination[1]) return true; + + maze[start[0]][start[1]] = 2; + for(int[] dir:dirs){ + int r = start[0]; + int c = start[1]; + + while(r>=0 && r=0 && c q = new LinkedList<>(); + q.offer(start); + maze[start[0]][start[1]] = 2; + + while(!q.isEmpty()){ + int[] curr = q.poll(); + if(curr[0] == destination[0] && curr[1] == destination[1]) return true; + + for(int[] dir:dirs){ + int r = curr[0]; + int c = curr[1]; + + while(r>=0 && r=0 && c