vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) { if (image[sr][sc] == newColor) return image; int oldColor = image[sr][sc]; int m = image.size(); int n = image[0].size(); automap = image; vector<pair<int, int>> dirs = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; stack<pair<int, int>> S; S.push({sr, sc}); while (!S.empty()) { auto p = S.top(); S.pop(); map[p.first][p.second] = newColor; for (auto dir : dirs) { int i = dir.first + p.first; int j = dir.second + p.second; if (i < 0 || i == m || j < 0 || j == n || map[i][j] != oldColor) continue; S.push({i, j}); } } returnmap; }

Distance to Nearest 0 in Matrix

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.