Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
- vector<vector<int>> result = image;
- int n = image.size();
- int m = image[0].size();
- vector<vector<bool>> visited(n, vector<bool>(m, false));
- int di[] = {0, 0, -1, 1};
- int dj[] = {-1, 1, 0, 0};
- queue<int> q;
- q.push(sr);
- q.push(sc);
- visited[sr][sc] = true;
- int s_color = image[sr][sc];
- result[sr][sc] = color;
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and image[ti][tj] == s_color) {
- q.push(ti);
- q.push(tj);
- visited[ti][tj] = true;
- result[ti][tj] = color;
- }
- }
- }
- return result;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement