/*
* The algorithm is based on identifying the position of element at (row, col) after rotation.
* Before Rotation Coordinates: row, col
* After Rotation Coordinates: r = col and c = N-1 –row (N => dimension of square matrix)
*
* 1 cyclic move cycle involvles:
* (0,0) -> (0,3) -> (3,3) - > (0,0)
* Values of location on left are propagated to locations on right of arrow.
*
* Firstly this is done for outer most layer of matrix.
* row =0 , col = N-1 , row = N-1 and col = 0.
* Then this is done for one layer inner to the outer most layer of matrix.
* Row = 1, col = N-2 row = N-2 and col = 1
* So , on till I < (N-1)/2.
* i.e. Half no of rows.
*
* Original Matrix
* 1 2 3 4
* 5 6 7 8
* 9 10 11 12
* 13 14 15 16
*
* After one cyclic move at i = 0, j =0
* 13 2 3 1
* 5 6 7 8
* 9 10 11 12
* 16 14 15 4
*
* After second cyclic move at i=0 , j=1
* 13 9 3 1
* 5 6 7 2
* 15 10 11 12
* 16 14 8 4
*
*
* Rotated Matrix
* 13 9 5 1
* 14 10 6 2
* 15 11 7 3
* 16 12 8 4
*
*
* ALGORITHM
* 1. Scan each row till N/2
* for i=0 to i=(N-1)/2
* 2. Pick each column one by one.
* for j =I to j = N-1-i
* 3. Call moveCyclic and pass starting coordinates.
* a. Repeat 4 times.
* b. Temp1 = value of current node position.
* c. Temp2 = value of target location of temp1.
* d. Set row = NewRowIndex and col = NewColIndex.
* e. Set matrix[row][col] = temp1 and temp1 = temp2.
*
* ALTERNATE SOLUTION
* 1. Take transpose of a matrix.
* 2. And then take mirror image of this matrix.
*
* COMPLEXITY
* Time : O(n2)
* Space: O(1)
*
*/
void setNewIndex(int &row, int &col, int N)
{
int temp = col;
col = N - 1 - row;
row = temp;
}
void moveCyclic(int row , int col , int N)
{
int temp1 = matrix[row][col];
int temp2;
for(int k=1; k<=4; k++)
{
setNewIndex(row,col, N);
temp2 = matrix[row][col];
matrix[row][col] = temp1;
temp1 = temp2;
}
}
int main()
{
int N = 6;
display(N);
for(int i=0; i<(N-1)/2 ; i++)
{
for( int j=i; j<N-1-i ; j++)
{
moveCyclic(i,j,N);
}
}
cout<<"\nAfter Rotation\n";
display(N);
cout<<"\n\n";
system("pause");
}
Hi mine is a similar solution for O(1) space and O(n^2) time complexity - http://k2code.blogspot.in/2014/03/rotate-n-n-matrix-by-90-degrees.html
ReplyDeleteIt isn't very clear for me how to pick the boundary for the loops, it's seems a little arbitrary...
ReplyDeleteThis blog contains standard problems.
ReplyDeleteThanks for this.