# [TCS] CodeVita Problem : Chakravyuha C-language Program

Problem : Chakravyuha

Problem: Chakravyuha

During the battle of Mahabharat, when Arjuna was far away in the battlefield, Guru Drona made a Chakravyuha formation of the Kaurava army to capture Yudhisthir Maharaj. Abhimanyu, young son of Arjuna was the only one amongst the remaining Pandava army who knew how to crack the Chakravyuha. He took it upon himself to take the battle to the enemies.

Abhimanyu knew how to get power points when cracking the Chakravyuha. So great was his prowess that rest of the Pandava army could not keep pace with his advances. Worried at the rest of the army falling behind, Yudhisthir Maharaj needs your help to track of Abhimanyu's advances. Write a program that tracks how many power points Abhimanyu has collected and also uncover his trail

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)

Constraints: 0 < N <=100

Sample Input and Output
SNo.InputOutput
12
1 2
4 3
Total Power points : 1
(0,0)
25
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Total Power points : 3
(0,0)
(4,2)
(3,2)

CodeVita 2017 Question: Chakravyuha
C-Language Program (MockVita2/chakravyuha.c)

#include <stdio.h>
#include <stdlib.h>

#define CHECK_POWER_POINT(a, m, n) \
if (a[m][n] % 11 == 0)         \
{                              \
printf("\n(%d,%d)", m, n); \
}

#define WRITE_CELL(a, m, n)              \
if ((a[m][n] = counter++) % 11 == 0) \
{                                    \
powerPoints++;                   \
}

int main()
{
int n, i, k = 0, l = 0, counter = 1, powerPoints = 1;
int a;

scanf("%d", &n);

int m1 = n;
int n1 = n;

while (k < m1 && l < n1)
{
for (i = l; i < n1; i++)
WRITE_CELL(a, k, i);
k++;

for (i = k; i < n1; i++)
WRITE_CELL(a, i, n1 - 1);
n1--;

if (k < m1)
{
for (i = n1 - 1; i >= l; i--)
WRITE_CELL(a, m1 - 1, i);
m1--;
}

if (l < n1)
{
for (i = m1 - 1; i >= k; i--)
WRITE_CELL(a, i, l);
l++;
}
}

for (m1 = 0; m1 < n; m1++)
{
for (n1 = 0; n1 < n; n1++)
{
printf("%d", a[m1][n1]);
if (n1 != n - 1)
printf("\t");
}

printf("\n");
}

printf("Total Power points : %d\n", powerPoints);
printf("(0,0)");

m1 = n1 = n;
k = 0, l = 0;

while (k < m1 && l < n1)
{
for (i = l; i < n1; i++)
CHECK_POWER_POINT(a, k, i);
k++;

for (i = k; i < n1; i++)
CHECK_POWER_POINT(a, i, n1 - 1);
n1--;

if (k < m1)
{
for (i = n1 - 1; i >= l; i--)
CHECK_POWER_POINT(a, m1 - 1, i);
m1--;
}

if (l < n1)
{
for (i = m1 - 1; i >= k; i--)
CHECK_POWER_POINT(a, i, l);
l++;
}
}

return 0;
}

Sources:
https://www.programminggeek.in/
https://github.com/

* Ask us, what you want?