TOPIC

Nao funciona. 5% error

Gustavo Cunha Lacerda asked 1 year ago

int main(void){
    int n, m, poki, pokj, ashi, ashj;
    while(scanf("%d %d", &n, &m) != EOF) {
        int a[n][m];
        for(int linha = 0; linha < n; linha++){
            for(int coluna = 0; coluna < m; coluna++){
                scanf("%d", &a[n][m]);
                if (a[n][m] == 2)
                {
                    poki = linha;
                    pokj = coluna;
                }
                else if (a[n][m] == 1)
                {
                    ashi = linha;
                    ashj = coluna;
                }
            }
        }
        int disti = abs(poki - ashi);
        int distj = abs(pokj - ashj);

        printf("%d\n", disti+distj);
    }
    return 0;
}

Remember not post solutions. Your post may be reviewed by our moderators.

  • feodorv replied 1 year ago

    What did you mean by it?

            int a[n][m];
            ...
            scanf("%d", &a[n][m]);

    You have here the out of bounds error. Why not simply

            int a;
            ...
            scanf("%d", &a);