Problem Statement
A Chakravyuha is a wheel-like formation. Pictorially it is depicted as below
A Chakravyuha has a very well-defined co-ordinate system. Each point on the co-ordinate system is manned by a certain unit of the army. The Commander-In-Chief is always located at the center of the army to better co-ordinate his forces. The only way to crack the Chakravyuha is to defeat the units in sequential order.
A Sequential order of units differs structurally based on the radius of the Chakra. The radius can be thought of as length or breadth of the matrix depicted above. The structure i.e. placement of units in sequential order is as shown below
The entry point of the Chakravyuha is always at the (0,0) co-ordinate of the matrix above. This is where the 1st army unit guards. From (0,0) i.e. 1st unit Abhimanyu has to march towards the center at (2,2) where the 25th i.e. the last of the enemy army unit guards. Remember that he has to proceed by destroying the units in sequential fashion. After destroying the first unit, Abhimanyu gets a power point. Thereafter, he gets one after destroying army units which are multiples of 11. You should also be a in a position to tell Yudhisthir Maharaj the location at which Abhimanyu collected his power points.
Input Format:
First line of input will be length as well as breadth of the army units, say N
Output Format:
- Print NxN matrix depicting the placement of army units, with unit numbers delimited by (\t) Tab character
- Print Total power points collected
- Print coordinates of power points collected in sequential fashion (one per line)
- Print coordinates of power points collected in sequential fashion (one per line)
Constraints:
0 < N <=100
The Logic:
#include<stdio.h>
int isDiv(int);
int main(){
int n;
scanf("%d",&n);
//check contraint
if(n<=0 || n>100){
return 0;
}
int a[100][100]; // main matrix
int counter=1; // keeps track of number
int i,j; //keeps track of position
int w; // w - window
int size=n-1; // size - size of segment of window
for( w = 0 ; w < n / 2 ; w++){
i = w;
j = w;
//go right
for( j = j ; j < size + w ; j++){
a[ i ][ j ] = counter;
counter++;
}
//go down
for( i = i ; i < size + w ; i++){
a[ i ][ j ] = counter;
counter++;
}
//go left
for( j = j ; j > w ; j--){
a[ i ][ j ] = counter;
counter++;
}
//go up
for( i = i ; i > w ; i--){
a[ i ][ j ] = counter;
counter++;
}
size = size-2;
}
if( n % 2 != 0){
a[ w ][ w ] = counter;
}
//print matrix
for( i = 0 ; i < n ; i++){
for( j = 0 ; j < n ; j++){
printf("%d\t", a[ i ][ j ]);
}
printf("\n");
}
int no_div = (n*n)/11 + 1 ; // no of divisibles
printf("Total Power points : %d\n",no_div);
printf("(0,0)\n");
size=n-1;
//print positions
for( w = 0 ; w < n / 2 ; w++){
i = w;
j = w;
//go right
for( j = j ; j < size + w ; j++){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)\n", i, j);
}
}
//go down
for( i = i ; i < size + w ; i++){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)\n", i, j);
}
}
//go left
for( j = j ; j > w ; j--){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)\n", i, j);
}
}
//go up
for( i = i ; i > w ; i--){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)\n", i, j);
}
}
size = size-2;
}
if( n % 2 != 0){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)\n", i, j);
}
}
return 0;
}
int isDiv(int x){
if( x % 11 == 0 ){
return 1;
}
else{
return 0;
}
}
The code is working fine as can be checked on Ideone.