指標_填滿圖形
user_3763047219
c_cpp
2 years ago
1.8 kB
9
Indexable
#include <stdio.h> #include <stdlib.h> void floodFill(int** image, int imageSize, int sr, int sc, int newColor) { int OriginColor = image[sr][sc]; if (sc - 1 > -1) { if (image[sr][sc - 1] == OriginColor) { image[sr][sc] = newColor; floodFill(image, imageSize, sr, sc - 1, newColor); } } if (sc + 1 < imageSize) { if (image[sr][sc + 1] == OriginColor) { image[sr][sc] = newColor; floodFill(image, imageSize, sr, sc + 1, newColor); } } if (sr - 1 > -1) { if (image[sr - 1][sc] == OriginColor) { image[sr][sc] = newColor; floodFill(image, imageSize, sr - 1, sc, newColor); } } if (sr + 1 < imageSize) { if (image[sr + 1][sc] == OriginColor) { image[sr][sc] = newColor; floodFill(image, imageSize, sr + 1, sc, newColor); } } image[sr][sc] = newColor; } 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; }
Editor is loading...