C Saved Program: quiescent-palace

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include <stdio.h>
#include <stdlib.h>

int main(){
 int l, c, i, j, soma=0;

 while(scanf("%d %d", &l, &c)!=EOF){
    int matriz[l][c];

    for(i=0; i<l; i++){
        for(j=0; j<c; j++){
           scanf("%d", &matriz[i][j]);
        }
    }

    for(i=0; i<l; i++){
        for(j=0; j<c; j++){
           if(matriz[i][j]==1){
              matriz[i][j]=9;
           }
        }
    }

    for(i=0; i<l; i++){
        for(j=0; j<c; j++){
         if(matriz[i][j]==0){
           if(matriz[i-1][j]==9 && i>=0){///Verificar acima
             soma++;
           }
           if(matriz[i+1][j]==9 && (i>=0 && i<l)){///Verificar abaixo
             soma++;
           }
           if(matriz[i][j-1]==9 && j>=0){///Verificar à esquerda
             soma++;
           }
           if(matriz[i][j+1]==9 && (j>=0 && j<c)){///Verificar à direita
             soma++;
           }

           matriz[i][j]=soma;
         }
           soma=0;
        }
    }


    for(i=0; i<l; i++){
        for(j=0; j<c; j++){
           printf("%d\t", matriz[i][j]);
        }
        printf("\n");
    }
    soma=0;
 }
 return 0;
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs