Rotate by 90 degree
PROBLEM :
Given an square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of the square matrix.The second line of each test case contains NxN space separated values of the matrix M.
Output:
Corresponding to each test case, in a new line, print the rotated array.
Constraints:
1 = T = 50
1 = N = 50
Example:
Input
1
3
1 2 3 4 5 6 7 8 9
Output
3 6 9 2 5 8 1 4 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 50
void rotate_90deg(int [MAX][MAX],int ) ;
int main()
{
int t,no,mat[MAX][MAX] ;
int i,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
rotate_90deg(mat,no) ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_90deg(int mat[MAX][MAX],int no)
{
int i,j ;
int m[no][no] ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
m[i][j]=mat[j][no-1-i] ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
mat[i][j]=m[i][j] ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(without using any extra space)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 50
void rotate_90deg(int [MAX][MAX],int ) ;
int main()
{
int t,no,mat[MAX][MAX] ;
int i,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
rotate_90deg(mat,no) ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_90deg(int mat[MAX][MAX],int no)
{
int i,j ;
int temp ;
for(i=0;i<no/2;i++)
{
for(j=i;j<no-i-1;j++)
{
temp=mat[i][j] ;
mat[i][j]=mat[j][no-i-1] ;
mat[j][no-i-1]=mat[no-i-1][no-j-1] ;
mat[no-i-1][no-j-1]=mat[no-j-1][i] ;
mat[no-j-1][i]=temp ;
}
}
}
---------------------------------------------------------------------------------
Given an square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of the square matrix.The second line of each test case contains NxN space separated values of the matrix M.
Output:
Corresponding to each test case, in a new line, print the rotated array.
Constraints:
1 = T = 50
1 = N = 50
Example:
Input
1
3
1 2 3 4 5 6 7 8 9
Output
3 6 9 2 5 8 1 4 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 50
void rotate_90deg(int [MAX][MAX],int ) ;
int main()
{
int t,no,mat[MAX][MAX] ;
int i,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
rotate_90deg(mat,no) ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_90deg(int mat[MAX][MAX],int no)
{
int i,j ;
int m[no][no] ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
m[i][j]=mat[j][no-1-i] ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
mat[i][j]=m[i][j] ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(without using any extra space)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 50
void rotate_90deg(int [MAX][MAX],int ) ;
int main()
{
int t,no,mat[MAX][MAX] ;
int i,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cin>>mat[i][j] ;
rotate_90deg(mat,no) ;
for(i=0;i<no;i++)
for(j=0;j<no;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}
return 0;
}
void rotate_90deg(int mat[MAX][MAX],int no)
{
int i,j ;
int temp ;
for(i=0;i<no/2;i++)
{
for(j=i;j<no-i-1;j++)
{
temp=mat[i][j] ;
mat[i][j]=mat[j][no-i-1] ;
mat[j][no-i-1]=mat[no-i-1][no-j-1] ;
mat[no-i-1][no-j-1]=mat[no-j-1][i] ;
mat[no-j-1][i]=temp ;
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment