# LeetCode: 733. 图像渲染¶

## 1、题目描述¶

输入:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2



• imageimage[0] 的长度在范围 [1, 50] 内。
• 给出的初始点将满足 0 <= sr < image.length0 <= sc < image[0].length
• image[i][j]newColor 表示的颜色值在范围 [0, 65535]内。

## 2、解题思路¶

void flood(int **image, int imageRowSize, int imageColSize, int sr, int sc, int oldColor, int newColor, int **sign) {

if (sign[sr][sc] == 0) {
sign[sr][sc] = 1;

if (oldColor == image[sr][sc]) {
image[sr][sc] = newColor;
} else {
return;
}
if (sr - 1 >= 0) {
flood(image, imageRowSize, imageColSize, sr - 1, sc, oldColor, newColor, sign);
}
if (sc - 1 >= 0) {
flood(image, imageRowSize, imageColSize, sr, sc - 1, oldColor, newColor, sign);
}
if (sr + 1 < imageRowSize) {
flood(image, imageRowSize, imageColSize, sr + 1, sc, oldColor, newColor, sign);
}
if (sc + 1 < imageColSize) {
flood(image, imageRowSize, imageColSize, sr, sc + 1, oldColor, newColor, sign);
}
} else {
return;
}

}

int **floodFill(int **image, int imageRowSize, int imageColSize, int sr, int sc, int newColor, int **columnSizes,
int *returnSize) {
*columnSizes = (int *) malloc(sizeof(int) * imageRowSize);
for (int i = 0; i < imageRowSize; i++) {
(*columnSizes)[i] = imageColSize;
}
*returnSize = imageRowSize;

int **result = (int **) malloc(sizeof(int *) * imageRowSize);
int **sign = (int **) malloc(sizeof(int *) * imageRowSize);
//    int sign[imageRowSize][imageColSize];

int *line;
for (int i = 0; i < imageRowSize; i++) {
line = (int *) malloc(sizeof(int) * imageColSize);
result[i] = line;
line = (int *) malloc(sizeof(int) * imageColSize);
sign[i] = line;
for (int j = 0; j < imageColSize; j++) {
result[i][j] = image[i][j];
sign[i][j] = 0;
}
}

flood(result, imageRowSize, imageColSize, sr, sc, image[sr][sc], newColor, sign);
return result;

}