Task Description
填滿圖形
有一張圖片以 n×n 的二維非負整數陣列 A 表示,A 中的元素不同數字代表不同顏色。往指定位置 (sr,sc) 倒入新顏色newColor,相連且相同顏色的格子都會變成新顏色。以 3×3 舉例如下:
0 0 1
1 1 0
1 0 3
若 (sr,sc)=(1,1) 且 newColor=2,表示與第二列第二行(0-based)相連且相同顏色的元素都會改成2,因此輸出如下:
0 0 1
2 2 0
2 0 3
請使用此函數原型 void floodFill(int** image, int imageSize, int sr, int sc, int newColor) 完成。
Hint
main.c
12345678910111213141516171819202122232425262728293031323334353637383940 #include <stdio.h>#include <stdlib.h> void floodFill(int** image, int imageSize, int sr, int sc, int newColor) { //add your code} int main() { int n; int sr, sc, newColor; scanf("%d", & n); int** p = NULL; p = (int**)malloc(sizeof(int*) * n); for (int i = 0; i < n; i++) { p[i] = (int*)malloc(sizeof(int) * n); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int tmp; scanf("%d", &tmp); p[i][j] = tmp; } } scanf("%d", &sr); scanf("%d", &sc); scanf("%d", &newColor); floodFill(p, n, sr, sc, newColor); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", p[i][j]); } printf("\n"); } for (int i = 0; i < n; i++) { free(p[i]); } free(p); return 0;}
Input Format
第一列輸入為 n,第2列至第 n+1 列輸入為 A 中的元素值。最後一列輸入依序為 sr、sc、 newColor。
Note: 0<n≤10、0≤A[i][j]≤255
Sample Input
123456 41 0 1 01 1 1 11 1 2 00 0 1 01 1 3
Sample Output
1234 3 0 3 03 3 3 33 3 2 00 0 1 0