LeetCode #1765

Map of Highest Peak

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
const int dy[] = {-1,1,0,0};
const int dx[] = {0,0,-1,1};
class Solution {
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        queue<pair<int, int>> Q;
        int N = (int)isWater.size(), M = (int)isWater[0].size();
        vector<vector<int>> used(N, vector<int>(M));
        for(int i = 0; i < N; ++i) {
            for(int j = 0; j < M; ++j) {
                if(isWater[i][j] == 1) {
                    Q.emplace(i, j);
                    used[i][j] = 1;
                    isWater[i][j] = 0;
                }
            }
        }
        while(!Q.empty()) {
            auto [y, x] = Q.front(); Q.pop();
            for(int k = 0; k < 4; ++k) {
                int qy = y + dy[k], qx = x + dx[k];
                if(0 > qy || qy >= N || 0 > qx || qx >= M || used[qy][qx]) continue;
                used[qy][qx] = 1;
                isWater[qy][qx] = isWater[y][x] + 1;
                Q.emplace(qy, qx);
            }
        }
        return isWater;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.