Labyrinth

You are given a map of a labyrinth, and your task is to find a path from start to end. You can walk left, right, up, and down.

Example:

Input: n = 5, m = 8, board = {{"########", "#.A#...#", "#.##.#B#", "#......#", "########"}};

Output:

YES 9 LDDRRRRRU

Approach:

C++

#include <bits/stdc++.h>
using namespace std;
int vis[1001][1001];
int dist[1001][1001];
int nm;
char grid[1001][1001];
int dx[] = {-1010};
vector<intparent[1001][1001];
int dy[] = {010, -1};
bool isValid(int xint y)
{
    if (x < 1 || x > n || y < 1 || y > m)
        return false;
    if (vis[x][y] == 1 || grid[x][y] == '#')
        return false;
    return true;
}
void bfs(int xint y)
{
    queue<pair<intint>> q;
    q.push({xy});
    vis[x][y] = 1;
    dist[x][y] = 0;
    parent[x][y= {-1, -1};
    while (!q.empty())
    {
        int currx = q.front().first;
        int curry = q.front().second;
        q.pop();
        for (int i = 0i < 4i++)
        {
            int newx = currx + dx[i];
            int newy = curry + dy[i];
            if (isValid(newxnewy))
            {
                vis[newx][newy] = 1;
                dist[newx][newy] = dist[currx][curry] + 1;
                parent[newx][newy= {currxcurry};
                q.push({newxnewy});
            }
        }
    }
}
int main()
{
    n = 5m = 8;

    vector<stringboard = {{"########",
                             "#.A#...#",
                             "#.##.#B#",
                             "#......#",
                             "########"}};
    int srcx = 1srcy = 1destx = 1desty = 1;
    for (int i = 1i <= ni++)
    {
        for (int j = 1j <= mj++)
        {
            grid[i][j] = board[i - 1][j - 1];
            if (grid[i][j] == 'A')
            {
                srcx = i;
                srcy = j;
            }
            else if (grid[i][j] == 'B')
            {
                destx = i;
                desty = j;
            }
        }
    }
    bfs(srcxsrcy);
    if (vis[destx][desty] == 0)
        cout << "NO\n";
    else
    {
        string ans = "";
        cout << "YES\n";
        int p = parent[destx][desty][0];
        int q = parent[destx][desty][1];

        cout << dist[destx][desty<< "\n";
        while (true)
        {
            if (p == -1 && q == -1)
                break;
            else
            {
                if (p == destx && q == desty + 1)
                {
                    ans += 'L';
                    desty += 1;
                    p = parent[destx][desty][0];
                    q = parent[destx][desty][1];
                }
                else if (p == destx && q == desty - 1)
                {
                    ans += 'R';
                    desty -= 1;
                    p = parent[destx][desty][0];
                    q = parent[destx][desty][1];
                }
                else if (p == destx - 1 && q == desty)
                {
                    ans += 'D';
                    destx -= 1;
                    p = parent[destx][desty][0];
                    q = parent[destx][desty][1];
                }
                else
                {
                    ans += 'U';
                    destx += 1;
                    p = parent[destx][desty][0];
                    q = parent[destx][desty][1];
                }
            }
        }
        reverse(ans.begin(), ans.end());
        cout << ans << "\n";
    }
    return 0;
}


No comments:

Post a Comment