This Saturday the Amazon CodeNinja Programming Contest is taking place. Here’s the first problem:
GIven a 2D NxN matrix, visualize it as concentric circles. You have to find the rotated matrix where each element in the circle is rotated by 1 position layer by layer in an alternate clockwise and anticlockwise direction.
Input Format:
First line of input contains an integer, N.
Then follow N lines, each containing N space separated integers. These numbers are the entries of given matrix.
Output Format:
Print to output the resultant matrix. Each row has to be printed in a separate line and within each row adjecent numbers should be separated by a space.
Sample Input:
4
2 3 4 5
1 6 7 8
4 2 1 9
5 3 2 4
Sample Output:
1 2 3 4
4 7 1 5
5 6 2 8
3 2 4 9
My solution in C is below (it solved all test cases):
#include <stdio.h>
int main(){
int n,i,j,x,circles;
int vector[1000][1000];
int temp,temp2;
scanf("%d",&n);
for (i=0;i<n;i++){
for (j=0;j<n;j++){
scanf("%d",&x);
vector[i][j]=x;
}
}
for (circles=0;circles<n/2;circles++){
temp = vector[circles][circles];
if (circles%2==0){
for (j=circles;j<n-1-circles;j++){
temp2 = vector[circles][j+1];
vector[circles][j+1] = temp;
temp = temp2;
}
for (i=circles;i<n-1-circles;i++){
temp2 = vector[i+1][n-1-circles];
vector[i+1][n-1-circles] = temp;
temp = temp2;
}
for (j=n-1-circles;j>circles;j--){
temp2 = vector[n-1-circles][j-1];
vector[n-1-circles][j-1] = temp;
temp = temp2;
}
for (i=n-1-circles;i>circles;i--){
temp2 = vector[i-1][circles];
vector[i-1][circles] = temp;
temp = temp2;
}
}
else{
for (i=circles;i<n-1-circles;i++){
temp2 = vector[i+1][circles];
vector[i+1][circles] = temp;
temp = temp2;
}
for (j=circles;j<n-1-circles;j++){
temp2 = vector[n-1-circles][j+1];
vector[n-1-circles][j+1] = temp;
temp = temp2;
}
for (i=n-1-circles;i>circles;i--){
temp2 = vector[i-1][n-1-circles];
vector[i-1][n-1-circles] = temp;
temp = temp2;
}
for (j=n-1-circles;j>circles;j--){
temp2 = vector[circles][j-1];
vector[circles][j-1] = temp;
temp = temp2;
}
}
}
for (i=0;i<n;i++){
for (j=0;j<n;j++){
printf("%d",vector[i][j]);
if (j<n-1)
printf(" ");
}
if (i<n-1)
printf("n");
}
return 0;
}
i hav a much shorter program i guess
(for anti-cloclwise rotation)
#include
#include
#include
#include
int min;
int main() {
long int r,c;
int i,j,m,x,w,z,f;
long int t,
a[101][101];
int rec(long int a[101][101],int r,int c,long int t);
scanf(“%ld%ld%ld”,&r,&c,&t);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
scanf("%ld",&a[i][j]);
if(c<r)
min=c;
else
min=r;
rec(a,r,c,t);
return 0;
}
int rec(long int a[101][101],int r,int c,long int t)
{
long int b[101][101];
int i,j,m,x,w,z,f;
if(t==0)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%ld ",a[i][j]);}printf("\n");
}return 0;
}
else
{
i=1;
while(i<=min/2)
{j=i;
while(j<(c-i+1)) //left
{
b[i][j]=a[i][j+1];
++j;
}
x=i;
while(x<(r+1-i))//down
{b[x+1][i]=a[x][i];
++x;
}
z=i;
while(z<(c+1-i))//right
{
b[r-i+1][z+1]=a[r-i+1][z];
++z;
}
z=i;
while(z<r+1-i)//up
{
b[z][c+1-i]=a[z+1][c+1-i];
++z;
}
++i;
}
}
return rec(b,r,c,t-1);
}