Flood Fill

2017/12/11 posted in  leetcode

class Solution {
public:
    void fillColor(vector<vector<int>>& image,int sr, int sc ,int tmp){
        if (sr>0&&image[sr-1][sc]==tmp) {
            image[sr-1][sc]=image[sr][sc];
            fillColor(image, sr-1, sc, tmp);
        }
        if (sr<image.size()-1&&image[sr+1][sc]==tmp) {
            image[sr+1][sc]=image[sr][sc];
            fillColor(image, sr+1, sc, tmp);
        }
        if (sc>0&&image[sr][sc-1]==tmp) {
            image[sr][sc-1]=image[sr][sc];
            fillColor(image, sr, sc-1, tmp);
        }
        if (sc<image[0].size()-1&&image[sr][sc+1]==tmp) {
            image[sr][sc+1]=image[sr][sc];
            fillColor(image, sr, sc+1, tmp);
        }
    }

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int tmp = image[sr][sc];
        if (tmp == newColor) {
            return image;
        }
        image[sr][sc] = newColor;
        fillColor(image, sr, sc, tmp);
        return image;
    }
};