Facebook - Phone Screen | Find Shortest Path With Obstacles In Grid | Dijkstra + BFS | Clean Code

1096
Dec 11, 2024
Dec 12, 2024
// Program to find the shortest path from source to destination cell in a grid ~ coded by Hiren
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;

class ShortestPath {
    vector<vector<int>> directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};
    int N, M;

    bool isValid(int R, int C) {
        return R >= 0 && C >= 0 && R < N && C < M;
    }

	// O(K) & O(K) : Where K = N*M
    vector<P> BFS(vector<vector<char>>& grid, int sourceR, int sourceC, int destR, int destC) {
        vector<vector<P>> parent(N, vector<P>(M));
        parent[sourceR][sourceC] = {-1,-1};

        vector<vector<int>> minDistance(N, vector<int>(M, INT_MAX));
        minDistance[sourceR][sourceC] = 0;

        queue<vector<int>> q;
        q.push({0, sourceR, sourceC});

        while(!q.empty()) {
            auto info = q.front(); q.pop();
            int currDistance = info[0];
            int R = info[1];
            int C = info[2];

            if(R == destR && C == destC)
                break; // If reached the destination cell

            for(auto& dir : directions) {
                int newR = R + dir[0]; 
                int newC = C + dir[1]; 
                if(newR == sourceR && newC == sourceC) continue;
                if(isValid(newR, newC) && grid[newR][newC] != 'X' && minDistance[newR][newC] >= currDistance + 1) {
                    minDistance[newR][newC] = currDistance + 1;
                    parent[newR][newC] = {R, C};
                    q.push({currDistance + 1, newR, newC});
                }
            }
        }

        if(minDistance[destR][destC] == INT_MAX)
            return {}; // If its impossible to reach destination cell

        // Build the shortest path from {destR, destC} to {sourceR, sourceC}
        vector<P> result;
        P coordinate = {destR, destC};
        while(coordinate.first != -1) {
            result.push_back(coordinate);
            coordinate = parent[coordinate.first][coordinate.second];
        }

        reverse(begin(result), end(result)); // Reverse to get the original order
        return result;
    }

	// O(KLogK) & O(K) : Where K = N*M
    vector<P> dijkstra(vector<vector<char>>& grid, int sourceR, int sourceC, int destR, int destC) {
        vector<vector<P>> parent(N, vector<P>(M));
        parent[sourceR][sourceC] = {-1,-1};

        vector<vector<int>> minDistance(N, vector<int>(M, INT_MAX));
        minDistance[sourceR][sourceC] = 0;

        priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;
        minHeap.push({0, sourceR, sourceC});

        while(!minHeap.empty()) {
            auto info = minHeap.top(); minHeap.pop();
            int currDistance = info[0];
            int R = info[1];
            int C = info[2];

            if(R == destR && C == destC)
                break; // If reached the destination cell

            for(auto& dir : directions) {
                int newR = R + dir[0]; 
                int newC = C + dir[1]; 
                if(newR == sourceR && newC == sourceC) continue;
                if(isValid(newR, newC) && grid[newR][newC] != 'X' && minDistance[newR][newC] >= currDistance + 1) {
                    minDistance[newR][newC] = currDistance + 1;
                    parent[newR][newC] = {R, C};
                    minHeap.push({currDistance + 1, newR, newC});
                }
            }
        }

        if(minDistance[destR][destC] == INT_MAX)
            return {}; // If its impossible to reach destination cell

        // Build the shortest path from {destR, destC} to {sourceR, sourceC}
        vector<P> result;
        P coordinate = {destR, destC};
        while(coordinate.first != -1) {
            result.push_back(coordinate);
            coordinate = parent[coordinate.first][coordinate.second];
        }

        reverse(begin(result), end(result)); // Reverse to get the original order
        return result;
    }

public:
    vector<P> shortestPath(vector<vector<char>>& grid) {
        N = grid.size(), M = grid[0].size();

        // Find the coordinates of source and destination cell
        int sourceR, sourceC, destR, destC;
        for(int R = 0; R < N; ++R) {
            for(int C = 0; C < M; ++C) {
                if(grid[R][C] == 'S') sourceR = R, sourceC = C; 
                else if(grid[R][C] == 'E') destR = R, destC = C;
            }
        }

        return dijkstra(grid, sourceR, sourceC, destR, destC);
    }
};

// Driver code
int main() {
    vector<vector<char>> grid = {{'1','1','1','1','1'}, 
                                 {'S','1','X','1','1'}, 
                                 {'1','1','1','1','1'}, 
                                 {'X','1','1','E','1'}, 
                                 {'1','1','1','1','X'}};
    ShortestPath obj;
    vector<P> result = obj.shortestPath(grid);

    cout<<"The shortest path is:\n";
    for(auto& [R, C] : result)
        cout<<R<<","<<C<<'\n';

    return 0;
}
// Link: https://leetcode.com/discuss/interview-question/301192/facebook-phone-screen-shortest-path-with-obstacles

𝗨𝗣𝗩𝗢𝗧𝗘 𝗜𝗙 𝗬𝗢𝗨 𝗟𝗜𝗞𝗘 𝗧𝗛𝗘 𝗦𝗢𝗟𝗨𝗧𝗜𝗢𝗡 👍

Note - This solutions are created by myself after reading this post:
Link: https://leetcode.com/discuss/interview-question/301192/facebook-phone-screen-shortest-path-with-obstacles

Comments (8)