(Leetcode) [Graph] Pacific Atlantic water flow

Question

m x n ์ง์‚ฌ๊ฐํ˜•์˜ ์„ฌ์ด ์žˆ๋Š”๋ฐ, ์ขŒ์ธก๊ณผ ์ƒ๋‹จ์€ ํƒœํ‰์–‘์œผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ์ ธ์žˆ๊ณ  ์šฐ์ธก๊ณผ ํ•˜๋‹จ์€ ๋Œ€์„œ์–‘์œผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ์ ธ์žˆ๋‹ค.
์„ฌ์€ ์ •์‚ฌ๊ฐํ˜•์˜ ์…€๋กœ ๋‚˜๋‰˜์–ด์ง„๋‹ค. heights๋ผ๋Š” ์ •์ˆ˜ ๋งคํŠธ๋ฆญ์Šค์—์„œ heights[r][c]์— ์ ํ˜€์žˆ๋Š” ์ •์ˆ˜๋Š” (r, c) ์ขŒํ‘œ์˜ ํ•ด๋ฐœ๊ณ ๋„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
์„ฌ์—๋Š” ๋น„๊ฐ€ ๋งŽ์ด ์˜ค๋Š”๋ฐ, ๊ธฐ์ค€ ์…€์˜ ๋™์„œ๋‚จ๋ถ์— ์žˆ๋Š” ์…€๋“ค์˜ ๋†’์ด๊ฐ€ ๊ธฐ์ค€ ์…€์˜ ๋†’์ด๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด ๋น„๊ฐ€ ํ˜๋Ÿฌ ๋‚ด๋ ค๊ฐ„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋Œ€์–‘์— ์ ‘ํ•ด์žˆ๋Š” ์…€๋“ค์—์„œ๋Š” ๋น„๊ฐ€ ๋ฌด์กฐ๊ฑด ๋Œ€์–‘์œผ๋กœ ํ˜๋Ÿฌ ๋‚ด๋ ค๊ฐ„๋‹ค.
์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ๋น—๋ฌผ์ด ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘ ๋‘ ๊ตฐ๋ฐ ๋‹ค๋กœ ํ˜๋Ÿฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์…€์˜ ์ขŒํ‘œ๋ฅผ result๋ผ๋Š” ๋ฒกํ„ฐ์— pushํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


My first approach

ํ•จ์ˆ˜ ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ฐ ์…€์ด Pacific Ocean์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” bool dfsPacific๊ณผ, Atlantic Ocean์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” bool dfsAtlantic. base case๋“ค์€ row์™€ column์˜ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌผ์ด Pacific์ด๋‚˜ Atlantic์— ๋„๋‹ฌํ–ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์„œ return trueํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  Pacific ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ขŒ&์ƒ ๋ฐฉํ–ฅ, Atlantic ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์šฐ&ํ•˜ ๋ฐฉํ–ฅ์˜ ์…€ ์ˆซ์ž์™€ ๋น„๊ตํ•ด์„œ ๋ฌผ์ด ์ด๋™ํ•  ์ˆ˜ ์—†์œผ๋ฉด return false ํ•˜๊ฒŒ๋” ์„ค๊ณ„๋ฅผ ํ•ด๋†จ๋‹ค.
์‚ฌ์‹ค ํ•จ์ˆ˜ ์ด๋ฆ„๋“ค์— dfs๋ฅผ ๋„ฃ๊ธด ํ–ˆ์ง€๋งŒ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ dfs๊ฐœ๋…์„ ์ž˜ ์‚ฌ์šฉํ•œ๊ฑฐ ๊ฐ™์ง€๋Š” ์•Š๋‹ค.

๋ฌธ์ œ์ 

์ผ๋‹จ ์ฝ”๋“œ๋Š” ๋Œ์•„๊ฐ„๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฌผ์˜ ๋ฐฉํ–ฅ์ด ๊ณ„์† ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๊ฐ€๋ฉด ๋„๋‹ฌํ•˜์ง€ ๋ชป ํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์™”๋‹ค๊ฐ”๋‹คํ•˜๋ฉฐ ์›€์ง์ด๋ฉด ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์ ์— ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์ด dfs๊ฐ€ ๋  ๊ฑฐ ๊ฐ™์ง€๋งŒ ๊ฐˆํ”ผ๋ฅผ ๋ชป ์žก๊ฒ ๋‹ค.


Solution

์–ด๊น€์—†์ด geeksforgeeks์—์„œ ํ•ด์„ค์„ ์ฝ์–ด๋ดค๋‹ค.
DFS๋‚˜ BFS ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด์„ค์—์„  BFS์— ๋Œ€ํ•œ ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ์ฐธ๊ณ ํ•ด์„œ ์“ด๋‹ค.
์ผ๋‹จ ๋‚ด๊ฐ€ ์ƒ๊ฐํ–ˆ๋˜ ์ผ๋‹จ Pacific๊ณผ Atlantic์„ ๋”ฐ๋กœ ๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

  1. Create two queues; each for storing the coordinates directly connected to the Pacific Ocean and the Atlantic Ocean.
  2. Create two vectors for each marking the visited coordinates during the BFS traversal from all the coordinates in two queues.
  3. Coordinates that are marked as visited in both the vectors are added in the result.
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
    vector<vector<int>> result;
    queue<pair<int, int>> qPac, qAtl;
    int row = heights.size();
    int col = heights[0].size();

    for (int i=0; i<col; i++) {
        qPac.push({0, i});
        qAtl.push({row-1, i});
    }
    for (int j=0; j<row-1; j++) {
        qPac.push({j+1, 0});
        qAtl.push({j, col-1});
    }

    vector<vector<bool>> visitedP(row, vector<bool>(col, false));
    vector<vector<bool>> visitedA(row, vector<bool>(col, false));

    BFS(heights, visitedP, qPac, row, col);
    BFS(heights, visitedA, qAtl, row, col);

    for (int i=0; i<row; i++) {
        for (int j=0; j<col; j++) {
            if (visitedP[i][j] && visitedA[i][j]) {
                result.push_back({i, j});
            }
        }
    }

    return result;
}

// function to check if coordinate (i, j) lies inside N*M matrix
bool safe(int i, int j, int row, int col) {
    if (i<0 || j<0 || i>=row || j>=col)
        return false;
    return true;
}

// perform BFS traversal and mark visited cells
void BFS(vector<vector<int>> heights, vector<vector<bool>> &visited, queue<pair<int, int>> q, int row, int col) {
    while (!q.empty()) {
        pair<int, int> cur = q.front();
        q.pop();
        visited[cur.first][cur.second] = true;

        // check right side of the cell and see if right cell can reach the current cell
        if (safe(cur.first+1, cur.second, row, col) && heights[cur.first+1][cur.second] >= heights[cur.first][cur.second]
        && visited[cur.first+1][cur.second]==false) {
            q.push({cur.first+1, cur.second});
            visited[cur.first+1][cur.second] = true;
        }
        // check left ...
        if (safe(cur.first-1, cur.second, row, col) && heights[cur.first-1][cur.second] >= heights[cur.first][cur.second]
        && visited[cur.first-1][cur.second]==false) {
            q.push({cur.first-1, cur.second});
            visited[cur.first-1][cur.second] = true;
        }
        // check down ...
        if (safe(cur.first, cur.second-1, row, col) && heights[cur.first][cur.second-1] >= heights[cur.first][cur.second]
        && visited[cur.first][cur.second-1]==false) {
            q.push({cur.first, cur.second-1});
            visited[cur.first][cur.second-1] = true;
        }
        // check up ...
        if (safe(cur.first, cur.second+1, row, col) && heights[cur.first][cur.second+1] >= heights[cur.first][cur.second]
        && visited[cur.first][cur.second+1]==false) {
            q.push({cur.first, cur.second+1});
            visited[cur.first][cur.second+1] = true;
        }
    }
}

pacific-atlantic-water-flow-drawing.jpg

Categories:

Updated:

Leave a comment