Advertisement
nikunjsoni

1765

Apr 28th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
  4.         int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
  5.         int rows = isWater.size(), cols = isWater[0].size();
  6.         bool vis[rows][cols];
  7.         memset(vis, 0, sizeof vis);
  8.         vector<vector<int>> dist(rows, vector<int>(cols));
  9.         queue<pair<int, int>> q;
  10.        
  11.         for(int i=0; i<rows; i++)
  12.             for(int j=0; j<cols; j++){
  13.                 if(isWater[i][j]){
  14.                     dist[i][j] = 0;
  15.                     vis[i][j] = true;
  16.                     q.push({i, j});
  17.                 }
  18.             }
  19.        
  20.         while(!q.empty()){
  21.             auto p = q.front();
  22.             q.pop();
  23.             for(int i=0; i<4; i++){
  24.                 int nextX = p.first+d[i][0], nextY = p.second+d[i][1];
  25.                 if(nextX < 0 || nextX >= rows || nextY < 0 || nextY >= cols || vis[nextX][nextY])
  26.                     continue;
  27.                 vis[nextX][nextY] = true;
  28.                 dist[nextX][nextY] = dist[p.first][p.second]+1;
  29.                 q.push({nextX, nextY});
  30.             }
  31.         }
  32.        
  33.         return dist;
  34.     }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement