From d124ce45b390783d8bc8f4aa888181cb3d569033 Mon Sep 17 00:00:00 2001 From: NitishLonka Date: Mon, 22 Sep 2025 20:05:24 -0400 Subject: [PATCH] Completed Graph-1 --- BallInMaze.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ FindJudge.java | 20 ++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 BallInMaze.java create mode 100644 FindJudge.java diff --git a/BallInMaze.java b/BallInMaze.java new file mode 100644 index 0000000..568a834 --- /dev/null +++ b/BallInMaze.java @@ -0,0 +1,47 @@ +import java.util.LinkedList; +import java.util.Queue; + +//Idea is to use bfs to traverse and mark the visited cells to avoid going through cycles +//Time Complexity:O(m*n) +//Space Complexity:O(m*n) +public class BallInMaze { + int[][] dirs; + int m,n; + public boolean findBall(int[][] maze, int[] start, int[] destination) { + this.dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1}}; + this.m = maze.length; + this.n = maze[0].length; + + Queue q = new LinkedList<>(); + q.add(new int[]{start[0], start[1]}); + maze[start[0]][start[1]] = -1; + + while(!q.isEmpty()) + { + int[] curr = q.poll(); + for(int[] dir: dirs) + { + int r = dir[0] + curr[0]; + int c = dir[1] + curr[1]; + + while(r>=0 && c>=0 && r