Untitled

 avatar
unknown
c_cpp
3 years ago
2.0 kB
7
Indexable
#include <stdio.h>
#include <stdlib.h>

void floodFill(int** image, int imageSize, int sr, int sc, int newColor) {
    
    int OriginColor = image[sr - 1][sc - 1];
    if (sc - 1 > 0) {
        if (image[sr - 1][sc - 2] == OriginColor && image[sr - 1][sc - 2] != newColor) {
            image[sr - 1][sc - 1] = newColor;
            floodFill(image, imageSize, sr, sc - 1, newColor);
        }
    }
    if (sc + 1 <= imageSize) {
        if (image[sr - 1][sc] == OriginColor && image[sr - 1][sc] != newColor) {
            image[sr - 1][sc - 1] = newColor;
            floodFill(image, imageSize, sr, sc + 1, newColor);
        }
    }
    if (sr - 1 > 0) {
        if (image[sr - 2][sc - 1] == OriginColor && image[sr - 2][sc - 1] != newColor) {
            image[sr - 1][sc - 1] = newColor;
            floodFill(image, imageSize, sr - 1, sc, newColor);
        }
    }
    if (sr + 1 <= imageSize) {
        if (image[sr][sc - 1] == OriginColor && image[sr][sc - 1] != newColor) {
            image[sr - 1][sc - 1] = newColor;
            floodFill(image, imageSize, sr + 1, sc, newColor);
        }
    }
    image[sr - 1][sc - 1] = 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...