[C++] Easy to Understand BFS - With diagrammatic explanation
\n\nSo Here the simple intuition is that we take a 2d answer matrix and initialize all its values to INT_MAX. Then when we find any water block (isWater[i][j] =
![image](https://assets.leetcode.com/users/images/6324a2cf-c61c-4b85-8dbd-03a07563dfee_1630095693.465843.jpeg)\n\nSo Here the simple intuition is that we take a 2d answer matrix and initialize all its values to INT_MAX. Then when we find any water block (isWater[i][j] == 1) then i will push those indexes inside the queue and mark the ans[i][j] = 0 as the water block should have height 0. Now my x contains the I index and y will contain the j index values. now we iterate in all 4 directions checking if the position is valid and if ans[xx][yy] (adjacent block) is greater than 1 + ans[x][y] (current block) we want absolute height difference to be at most 1 so we check this condition and update the value and pushing the new block index to the queue so that its adjacent elements get filled up. Lastly, we return the ans vector. \n\n```\nclass Solution {\npublic:\n    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {\n        int r = isWater.size();\n        int c = isWater[0].size();\n        vector<vector<int>> ans(r,vector<int>(c,INT_MAX));\n        queue<pair<int,int>> q;\n        \n        for(int i = 0;i<r;i++){\n            for(int j = 0;j<c;j++){\n                if(isWater[i][j] == 1){\n                    q.push({i,j});\n                    ans[i][j] = 0;\n                }\n            }\n        }\n        \n        while(!q.empty()){\n            int x = q.front().first;\n            int y = q.front().second;\n            q.pop();\n            int dx[4] = {0,0,-1,1};\n            int dy[4] = {-1,1,0,0};\n            \n            for(int i = 0;i<4;i++){\n                int xx = x + dx[i];\n                int yy = y + dy[i];\n                \n                if(xx>=0 && xx<r && yy>=0 && yy<c && ans[xx][yy] > 1 + ans[x][y]){\n                    ans[xx][yy] = 1 + ans[x][y];\n                    q.push({xx,yy});\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n\n**Upvote if this helps you :)**
['Breadth-First Search', 'C', 'C++']