要小心原本就跟target color 一樣的case, 也必須return掉, 不然它會生生不息
愛注意哦~~~
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void iterate(int** image, int row, int col,
int sr, int sc, int tar_color, int org_pixel) {
if (sr <0 || sr >=row || sc <0 || sc >= col)
return ;
if ((image[sr][sc]!=org_pixel) || (tar_color== org_pixel))
return ;
image[sr][sc]=tar_color;
iterate(image, row, col, sr-1, sc,tar_color, org_pixel);
iterate(image, row, col, sr+1, sc,tar_color, org_pixel);
iterate(image, row, col, sr, sc-1,tar_color, org_pixel);
iterate(image, row, col, sr, sc+1,tar_color, org_pixel);
return ;
}
int** floodFill(int** image, int imageSize, int* imageColSize, int sr, int sc, int color, int* returnSize, int** returnColumnSizes) {
*returnSize = imageSize;
*returnColumnSizes = imageColSize;
int org_pixel = image[sr][sc];
iterate(image, imageSize, *imageColSize, sr, sc,color, org_pixel);
return image;
}
沒有留言:
張貼留言