Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int maxDistance(vector<vector<int>>& g, int steps = 0) {
- queue<pair<int, int>> q, q1;
- for(int i = 0; i < g.size(); ++i)
- for(int j = 0; j < g[i].size(); ++j)
- if(g[i][j] == 1)
- q.push({ i - 1, j }), q.push({ i + 1, j }), q.push({ i, j - 1 }), q.push({ i, j + 1 });
- while(!q.empty()) {
- ++steps;
- while(!q.empty()){
- int i = q.front().first, j = q.front().second;
- q.pop();
- if(i >= 0 && j >= 0 && i < g.size() && j < g[i].size() && g[i][j] == 0) {
- g[i][j] = steps;
- q1.push({ i - 1, j }), q1.push({ i + 1, j }), q1.push({ i, j - 1 }), q1.push({ i, j + 1 });
- }
- }
- swap(q1, q);
- }
- return steps == 1 ? -1 : steps - 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement