Number of decisions to reach destination

Given a grid which consists of 4 types of characters : ‘B’ ‘.’ ‘S’ and ‘D’. We need to reach D starting from S, at each step we can go to neighboring cells i.e. up, down, left and right. Cells having character ‘B’ are blocked i.e. at any step we can’t move to cell having ‘B’. Given grid has dots in such a way that there is only one way to reach any cell from any other cell. We need to tell how many times we need to choose our way from more than one choices i.e. decide the path to reach D.

Examples:

Input : Grid = [".BBB.B.BB"
                ".....B.B."
                "B.B.B.BSB"
                ".DB......"]
Output : 4
In above shown grid we have to decide 4
times to reach destination at (3, 7), 
(3, 5), (1, 3) and (1, 1).  



We can solve this problem using DFS. In path from source to destination we can see that whenever we have more than 1 neighbors, we need to decide our path so first we do a DFS and store the path from source to the destination in terms of child-parent array and then we move from destination to source, cell by cell using parent array and at every cell where we have more than 1 neighbors we will increase our answer by 1.
Please see below code for better understanding.

// C++ program to find decision taken to
// reach destination from source
#include <bits/stdc++.h>
using namespace std;

//  Utility dfs method to fill parent array
void dfs(int u, vector<int> g[], int prt[], bool visit[])
{
    visit[u] = true;

    //  loop over all unvisited neighbors
    for (int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if (!visit[v])
        {
            prt[v] = u;
            dfs(v, g, prt, visit);
        }
    }
}

// method returns decision taken to reach destination
// from source
int turnsToReachDestination(string grid[], int M)
{
    int N = grid[0].length();

    //  storing direction for neighbors
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, -1, 0, 1};

    vector<int> g[M*N];
    bool visit[M*N] = {0};
    int prt[M*N];
    int start, dest;

    /*  initialize start and dest and
        store neighbours vector g
        If cell index is (i, j), then we can convert
        it to 1D as (i*N + j)  */
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (grid[i][j] == 'D')
                dest = i*N + j;
            if (grid[i][j] == 'S')
                start = i*N + j;

            g[i*N + j].clear();
            if (grid[i][j] != 'B')
            {
                for (int k = 0; k < 4; k++)
                {
                    int u = i + dx[k];
                    int v = j + dy[k];

                    // if neighboring cell is in boundry
                    // and doesn't have 'B'
                    if (u >= 0 && u < M && v >= 0 &&
                        v < N && grid[u][v] != 'B')
                        g[i*N + j].push_back(u*N + v);
                }
            }
        }
    }

    //  call dfs from start and fill up parent array
    dfs(start, g, part, visit);

    int curr = dest;
    int res = 0;

    //  loop from destination cell back to start cell
    while (curr != start)
    {
        /*  if current cell has more than 2 neighbors,
            then we need to decide our path to reach S
            from D, so increase result by 1 */
        if (g[curr].size() > 2)
            res++;

        curr = prt[curr];
    }

    return res;
}

//  Driver code to test above methods
int main()
{
    string grid[] =
    {
        ".BBB.B.BB",
        ".....B.B.",
        "B.B.B.BSB",
        ".DB......"
    };
    int M = sizeof(grid)/sizeof(grid[0]);
    cout << turnsToReachDestination(grid, M) << endl;
    return 0;
}

Output:

4

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.