Shortest Possible Path in a Maze

You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.

Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.

For example, given the following board:

[[f, f, f, f],
[t, t, f, t],
[f, f, f, f],
[f, f, f, f]]

and start = (3, 0) (bottom left) and end = (0, 0) (top left), the minimum number of steps required to reach the end is 7 since we would need to go through (1, 2) because there is a wall everywhere else on the second row.

Example:

Input:  board = {{1, 1, 1, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}, {1, 1, 1, 1}}, srcX = 3, srcY = 0, destX = 0, destY = 0
Output: 7

Approach

C++

#include <bits/stdc++.h>
using namespace std;
bool isValid(vector<vector<bool>> &board,
             int Mint N,
             vector<vector<bool>> visited,
             int iint j)
{
    return !(i < 0 || j < 0 || i >= M || j >= N ||
             !board[i][j] || visited[i][j]);
}

int ShortestPathInAMaze(vector<vector<bool>> &board,
                        int srcXint srcY,
                        int destXint destY)
{
    int M = board.size();
    int N = M > 0 ? board[0].size() : 0;

    vector<vector<bool>> visited(Mvector<bool>(Nfalse));

    queue<vector<int>> q;

    if (isValid(boardMNvisitedsrcXsrcY))
    {
        q.push({srcXsrcY0});
        visited[srcX][srcY] = true;
    }

    while (!q.empty())
    {
        vector<inttempElement = q.front();
        q.pop();

        if (tempElement[0] == destX && tempElement[1] == destY)
        {
            return tempElement[2];
        }

        // checking if the 4 elements are valid or
        //not and pushing in the queue

        if (isValid(boardMN,
                    visitedtempElement[0] + 1tempElement[1]))
        {
            q.push({tempElement[0] + 1tempElement[1],
                    tempElement[2] + 1});
        }

        if (isValid(boardMNvisited,
                    tempElement[0] - 1tempElement[1]))
        {
            q.push({tempElement[0] - 1tempElement[1],
                    tempElement[2] + 1});
        }

        if (isValid(boardMNvisited,
                    tempElement[0]tempElement[1] + 1))
        {
            q.push({tempElement[0]tempElement[1] + 1,
                    tempElement[2] + 1});
        }

        if (isValid(boardMNvisited,
                    tempElement[0]tempElement[1] - 1))
        {
            q.push({tempElement[0]tempElement[1] - 1,
                    tempElement[2] + 1});
        }
    }

    // -1 indicates no path exists.
    return -1;
}

int main()
{
    vector<vector<bool>> board = {{1111},
                                  {0010},
                                  {1111},
                                  {1111}};

    int srcX = 3srcY = 0;
    int destX = 0destY = 0;
    cout << ShortestPathInAMaze(boardsrcXsrcYdestXdestY);

    return 0;
}


No comments:

Post a Comment