Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pulse

Rotacionar matriz em 90 graus

Recommended Posts

Preciso criar um algoritmo para rotacionar uma matriz em 90 graus, sem utilizar matrizes auxiliares.

Tentativa:

#include <stdio.h>
int main ()
{
    int i, j, original = 0, linha, coluna, aux, N = 10, l, aux2, c, t;
    int mat[N][N];

    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        {
            mat[i][j] = original;
            original++;
        }
    }

    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }


        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");


    for(l=0; l<N; l++)
    {
        for(c=0; c<N; c++)
        {
            t = mat[l][c];
            mat[l][c] = mat[c][N-l-1];
            mat[c][N-l-1] = mat[N-l-1][N-c-1];
            mat[N-l-1][N-c-1] = mat[N-c-1][l];
            mat[N-c-1][l] = t;
        }
    }

    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

Entretanto a matriz final é idêntica a matriz original: http://ideone.com/m42bK4

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.