#include #include #define COR_AGUA 0 /* * P R O T O T I P O S */ void mostreUso (char *nomeProg); void *mallocSafe (unsigned int n); int afundaIlha(int x, int y, int **img); /* * M A I N */ int main(int argc, char *argv[]) { FILE *entrada; int **img; int nL, nC; int x, y; int nPixels; if (argc != 2) { mostreUso(argv[0]); exit(EXIT_FAILURE); } if ((entrada = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"%s: não foi possivel abrir o arquivo '%s'\n", argv[0], argv[1]); exit(EXIT_FAILURE); } fscanf(entrada,"%d %d", &nL, &nC); img = malloc(nL*sizeof(int*)); for (x = 0; x < nL; x++) img[x] = malloc(nC*sizeof(int)); for (x = 0; x < nL; x++) for (y = 0; y < nC; y++) fscanf(entrada,"%d", &img[x][y]); fclose(entrada); printf("Imagem original:\n"); for (x = 0; x < nL; x++) { for (y = 0; y < nC; y++) printf("%2d ", img[x][y]); printf("\n"); } printf("\nDigite a posicao do pixel: "); scanf("%d %d", &x, &y); nPixels = afundaIlha(x, y, img); printf("\nNumero de pixels na ilha do pixel (%d,%d) = %d\n", x, y, nPixels); printf("Nova imagem:\n"); for (x = 0; x < nL; x++) { for (y = 0; y < nC; y++) printf("%2d ", img[x][y]); printf("\n"); } return 0; } int afundaIlha(int x, int y, int **img) { int nPixels; if (img[x][y] == COR_AGUA) return 0; img[x][y] = COR_AGUA; nPixels = 1; nPixels += afundaIlha(x-1,y,img); nPixels += afundaIlha(x,y-1,img); nPixels += afundaIlha(x+1,y,img); nPixels += afundaIlha(x,y+1,img); return nPixels; } /* * mostra a meneira de usar programa */ void mostreUso (char *nomeProg) { fprintf(stdout,"Uso: %s \n", nomeProg); } void * mallocSafe (unsigned int n) { void *p; p = malloc(n); if (p == NULL) { fprintf(stderr,"Malloc de %u bytes falhou.\n", n); exit (-1); } return p; }