0
안녕하세요, 저는 C++ 문제를 풀기 시작했습니다. 그 중 하나는 N x N 행렬을 시계 방향으로 90도 회전시키는 것입니다.N x N 행렬의 회전 논리 이해하기
아래 코드 링크는 내가 언급 한 것입니다. 나는 C++/any에서 행렬 문제를 해결하지 못했다.
#include <stdio.h>
#include <stdlib.h>
void displayMatrix(unsigned int const *p, unsigned int row, unsigned int col);
void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col);
int main()
{
// declarations
unsigned int image[][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
unsigned int *pSource;
unsigned int *pDestination;
unsigned int m, n;
// setting initial values and memory allocation
m = 3, n = 4, pSource = (unsigned int *)image;
pDestination = (unsigned int *)malloc(sizeof(int)*m*n);
// process each buffer
displayMatrix(pSource, m, n);
rotate(pSource, pDestination, m, n);
displayMatrix(pDestination, n, m);
free(pDestination);
getchar();
return 0;
}
void displayMatrix(unsigned int const *p, unsigned int r, unsigned int c)
{
unsigned int row, col;
printf("\n\n");
for(row = 0; row < r; row++)
{
for(col = 0; col < c; col++)
{
printf("%d\t", *(p + row * c + col)); // what is this??? couldnt understand this logic?
}
printf("\n");
}
printf("\n\n");
}
void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col)
{
unsigned int r, c;
for(r = 0; r < row; r++)
{
for(c = 0; c < col; c++)
{
*(pD + c * row + (row - r - 1)) = *(pS + r * col + c); // not understanding this logic as well.
}
}
}
http://www.geeksforgeeks.org/turn-an-image-by-90-degree/
어느 한이 논리에 대한 자세한 내용을 설명해주십시오 수 있습니다. 위에서 언급 한 코드 자체에서 언급 한 몇 가지 문제를 해결할 수 없습니다.또한 시간과 공간의 복잡성에 대해 자세히 알려주십시오 .. 미리 감사드립니다.
에 해당합니다. – stark
@ molbdnilo : 답변 주셔서 감사합니다. 그러나 아직도 couldnt 한 양쪽 계산서와 관련된다. 당신은 이것을 더 많이 설명 할 수 있냐? 또한이 문장에 대해 설명해주십시오. printf ("% d \ t", * (p + row * c + col)); –