Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
- int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
- int rows = isWater.size(), cols = isWater[0].size();
- bool vis[rows][cols];
- memset(vis, 0, sizeof vis);
- vector<vector<int>> dist(rows, vector<int>(cols));
- queue<pair<int, int>> q;
- for(int i=0; i<rows; i++)
- for(int j=0; j<cols; j++){
- if(isWater[i][j]){
- dist[i][j] = 0;
- vis[i][j] = true;
- q.push({i, j});
- }
- }
- while(!q.empty()){
- auto p = q.front();
- q.pop();
- for(int i=0; i<4; i++){
- int nextX = p.first+d[i][0], nextY = p.second+d[i][1];
- if(nextX < 0 || nextX >= rows || nextY < 0 || nextY >= cols || vis[nextX][nextY])
- continue;
- vis[nextX][nextY] = true;
- dist[nextX][nextY] = dist[p.first][p.second]+1;
- q.push({nextX, nextY});
- }
- }
- return dist;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement