Posted on

Description

Submission

class Solution {
    using pii = pair<int, int>;
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int m = isWater.size();
        int n = isWater[0].size();
        vector<vector<int>> rets(m, vector<int>(n, 0));

        queue<pii> q;

        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                if(isWater[i][j]) q.push({i, j});
            }
        }

        const int dirs[] = {1, 0, -1, 0, 1};

        int height = 0;
        while(!q.empty()) {
            int cnt = q.size();
            
            while(cnt--) {
                auto [x, y] = q.front();
                q.pop();
                
                if(rets[x][y]) continue;
                rets[x][y] = height;

                for(int i = 0; i < 4; ++i) {
                    int x2 = x + dirs[i];
                    int y2 = y + dirs[i+1];

                    if(x2 < 0 || x2 >= m || y2 < 0 || y2 >= n || 
                        isWater[x2][y2] || rets[x2][y2]) continue;

                    q.push({x2, y2});
                }
            }

            ++height;
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *