Monday, 14 November 2016

Clockwise block breaker

Problem : Clockwise Bricks Breaker

Clockwise Bricks Breaker is a game where there is a ball which is used to break the block of bricks. Brick's Blocks are always in a form of a square matrix( e.g 2X2, 4X4). The ball has some characteristics like for breaking each brick from the block. It consumes 1 unit of energy and the whole setup is in a manner that the ball will break bricks always - in clock wise direction, in a ring fashion and start from top-left. Take an example of 4X4 brick block. 
 
        Figure 1. 


 
        Figure 2. 


 
        Figure 3. 
The numbers on the bricks are nothing but points gained by breaking that brick which is assigned row wise starting from 1 to NxN. The game gets over when energy of ball exhausts and once ball initiated with some energy it will only stop when all its energy exhaust. 

Your Task is to calculate total points (points of a block should be the positional value of that block as mentioned in the picture: You have to add points of all the broken bricks) gained by a player, where the energy of ball and size of block will be given. 


Input Format: 

First line of input should be the number of test cases T

Next T lines contain two variables N and E delimited by a whitespace where, 
  • N =size of square block (i.e. block will be of NXN bricks)
  • E =Energy of ball



Output Format: 

Output will be T lines containing the integer which is total number points gained by that energy. 
OR 

Print "Invalid Input" if constraint fails 
Constraints:
  • 0< T<=10
  • 0< N <=200
  • 0<= E <=N*N


Examp
EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUTEXPLAINATION
12
4 5
1 50

36
Invalid Input
For first test case as with Energy 5, the ball will break 5 bricks having points 1,4,16,13,2

So 1+4+16+13+2=36.
Second test case is Invalid Input because constraint E<=N*N not satisfied.
22
5 0
9 -3

0
Invalid Input
For first test case it is 0 as energy is zero.

For second test case it is Invalid Input because constraint E >= 0 not satisfied.

SOLUTION :

#include<iostream>
using namespace std;
int main()
{
int N,P;
cout<<"Enter N & Power :";
cin>>N>>P;
if(P>N*N)
P=N*N;
int wall[N][N],temp=0;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
temp++,wall[i][j]=temp;
int sum=0,
    i[4]={0,0,N-1,N-1},
    j[4]={0,N-1,N-1,0};
    temp=0;
while(P>0)
{
if(temp%4==0)
{
if(wall[i[0]][j[0]]==0)
{
i[0]+=1;
j[0]=i[0];
sum+=wall[i[0]][j[0]];
wall[i[0]][j[0]]=0;
j[0]+=1;
}
else
{
sum+=wall[i[0]][j[0]];
wall[i[0]][j[0]]=0;
j[0]+=1;
}
}
if(temp%4==1)
{
if(wall[i[1]][j[1]]==0)
{
j[1]-=1;
i[1]=i[0];
sum+=wall[i[1]][j[1]];
wall[i[1]][j[1]]=0;
i[1]+=1;
}
else
{
sum+=wall[i[1]][j[1]];
wall[i[1]][j[1]]=0;
i[1]+=1;
}
}
if(temp%4==2)
{
if(wall[i[2]][j[2]]==0)
{
i[2]-=1;
j[2]=i[2];
sum+=wall[i[2]][j[2]];
wall[i[2]][j[2]]=0;
j[2]-=1;
}
else
{
sum+=wall[i[2]][j[2]];
wall[i[2]][j[2]]=0;
j[2]-=1;
}
}
if(temp%4==3)
{
if(wall[i[3]][j[3]]==0)
{
j[3]+=1;
i[3]=i[2];
sum+=wall[i[3]][j[3]];
wall[i[3]][j[3]]=0;
i[3]-=1;
}
else
{
sum+=wall[i[3]][j[3]];
wall[i[3]][j[3]]=0;
i[3]-=1;
}
}
temp+=1;
P--;
}
cout<<"sum="<<sum;
}