Posts Find whether path exist (geeksforgeeks - SDE Sheet)
Post
Cancel

Find whether path exist (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a grid of size n*n filled with 0, 1, 2, 3. Check whether there is a path possible from the source to destination. You can traverse up, down, right and left. The description of cells is as follows:

  • A value of cell 1 means Source.
  • A value of cell 2 means Destination.
  • A value of cell 3 means Blank cell.
  • A value of cell 0 means Wall (blocked cell which we cannot traverse).

Note: There are only a single source and a single destination.

geeksforgeeks

SOLUTION

This can be solved using DFS.

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
69
70
class Solution {

    public boolean is_Possible(int[][] grid) {

        int n = grid.length;
        int m = grid[0].length;

        // Initialize source and destination coordinates
        int si = -1, sj = -1, di = -1, dj = -1;

        // get the source and destination
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    si = i;
                    sj = j;
                } else if (grid[i][j] == 2) {
                    di = i;
                    dj = j;
                }

                // Break if both source and destination are found
                if (si != -1 && di != -1) {
                    break;
                }
            }
        }

        // Perform depth-first search (DFS) to check if a path exists
        return dfs(grid, si, sj, di, dj);

    }

    public boolean dfs(int[][] grid, int si, int sj, int di, int dj) {

        // Check if the current cell is out of bounds or a wall
        if (di < 0 || dj < 0 || di >= grid.length || dj >= grid[0].length || grid[di][dj] == 0) {
            return false;
        }

        // If the current cell is the source cell, path is found
        if (di == si && dj == sj) {
            return true;
        }

        // Otherwise, mark the current cell as visited by setting it to 0
        grid[di][dj] = 0;

        // possible moves (right, left, down, up)
        int[] x = {0, 0, 1, -1};
        int[] y = {1, -1, 0, 0};

        // init:
        boolean reached = false;

        // Explore all four possible directions
        for (int k = 0; k < 4; k++) {

            int nextX = di + x[k];
            int nextY = dj + y[k];

            // Recursively perform DFS on the next cell
            // reached will be set to true if any of the moves returns true
            reached |= dfs(grid, si, sj, nextX, nextY);

        }

        return reached;
    }
}
This post is licensed under CC BY 4.0 by the author.