Abhishek is playing a game.The game is played on a rectangular grid consisting of N rows and M columns . Initially all the cells of the grid are uncoloured.
Abhishek's initial score is zero. At each turn , he chooses score cell that is yet not coloured and colors that cell. The score obtained in this step will be number of neighbouring coloured cells of the cell that Abhishek coloured in this step . The game will  end when all the cells are coloured .Finally total score obtained at the end of the game will be sum of score obtained in each turn.
Find the maximum score.

Input

2

2 2

3 2
Output
4
7
 Solution

 import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner kb=new Scanner(System.in);
int n;
n=kb.nextInt();
int m=0;
int sum=0;
while(m<n)
{sum=0;
int r=kb.nextInt();
int c=kb.nextInt();
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i==0 & j==0)
{
continue;
}
else{
if(j==0)
{sum=sum+1;}
else if(i==0)
{sum=sum+1;}
else
{
sum=sum+2;
}
}
}
}
System.out.println(""+sum);
m=m+1;
}
}
}

if you have any problem in this code, take a screen shot and contact with Rishabh Chaubey.

Post a Comment

Previous Post Next Post