BoardProblem code: TECH13 |
You have been given a 4x4 board which consists of integers from 1 to 50. A two player game is played on this board with the following rules :
- The first player can pick any number from the board, this is added to his personal total.
- Now the next person can only pick a number that was adjacent to the number picked by the earlier player. (Two cells are adjacent if they share a side). If all the adjacent cells are empty, the player can pick a number from any cell.
- The game ends when all the numbers have been picked from the board, i.e. in exactly 16 moves.
You task in this problem is given a board configuration, return the maximal difference in the personal totals of player 1 and player 2 , assuming they play optimally.
Input
The first line of input would consist of a single integer 'T' denoting the number of test cases, T < 1000. This is followed by 4T lines , where each set of 4 lines describe the board configuration with space separated integers. Each test case would be separated by a new line.
Output
You have to output a single integer denoting the maximal difference in the personal totals of player 1 and player 2 that is possible.
Example
Input: 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 1 1 2 1 1 1 1 1 Output: 0 3
| Author: | technovanza10 |
| Date Added: | 24-01-2010 |
| Time Limit: | 5 sec |
| Source Limit: | 50000 Bytes |
| Languages: | C, C99 strict, CLOJ, CPP 4.0.0-8, CPP 4.3.2, F#, GO, JAVA, PERL6, PYTH 3.1.2, TEXT |
Comments

Fetching successful submissions

class Main{ public static
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader br = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s="";int t=0,ctr=0,in=0,max=0,h=0,k=0;
int a[][]=new int[4][4];
do
{
t=Integer.parseInt(br.readLine());
}while(t>=1000);
for(int i=0;i<t;i++)
{
do
{
s=br.readLine().trim();
while(in!=-1)
{
in=s.indexOf(' ',in+1);
ctr++;
}
ctr--;
if(ctr!=3)
s="";
in=s.indexOf(" ");
max=a[0][0]=Integer.parseInt(s.substring(0,in));
if((a[0][1]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[0][1];
h=0;
k=1;
}
in=s.indexOf(' ',in+1);
if((a[0][2]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[0][2];
h=0;
k=2;
}
in=s.indexOf(' ',in+1);
if((a[0][3]=Integer.parseInt(s.substring(in+1)))>max)
{
max=a[0][3];
h=0;
k=3;
}
}while(s.length()<1);
in=0;ctr=0;
do
{
s=br.readLine().trim();
while(in!=-1)
{
in=s.indexOf(' ',in+1);
ctr++;
}
ctr--;
if(ctr!=3)
s="";
in=s.indexOf(" ");
if((a[1][0]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[1][0];
h=1;
k=0;
}
if((a[1][1]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[1][1];
h=1;
k=1;
}
in=s.indexOf(' ',in+1);
if((a[1][2]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[1][2];
h=1;
k=2;
}
in=s.indexOf(' ',in+1);
if((a[1][3]=Integer.parseInt(s.substring(in+1)))>max)
{
max=a[1][3];
h=1;
k=3;
}
}while(s.length()<1);
in=0;ctr=0;
do
{
s=br.readLine().trim();
while(in!=-1)
{
in=s.indexOf(' ',in+1);
ctr++;
}
ctr--;
if(ctr!=3)
s="";
in=s.indexOf(" ");
if((a[2][0]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[2][0];
h=2;
k=0;
}
if((a[2][1]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[2][1];
h=2;
k=1;
}
in=s.indexOf(' ',in+1);
if((a[2][2]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[2][2];
h=2;
k=2;
}
in=s.indexOf(' ',in+1);
if((a[2][3]=Integer.parseInt(s.substring(in+1)))>max)
{
max=a[2][3];
h=2;
k=3;
}
}while(s.length()<1);
in=0;ctr=0;
do
{
s=br.readLine().trim();
while(in!=-1)
{
in=s.indexOf(' ',in+1);
ctr++;
}
ctr--;
if(ctr!=3)
s="";
in=s.indexOf(" ");
if((a[3][0]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[3][0];
h=3;
k=0;
}
if((a[3][1]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[3][1];
h=3;
k=1;
}
in=s.indexOf(' ',in+1);
if((a[3][2]=Integer.parseInt(s.substring(in,(s.indexOf(' ',in+1))).trim()))>max)
{
max=a[3][2];
h=3;
k=2;
}
in=s.indexOf(' ',in+1);
if((a[3][3]=Integer.parseInt(s.substring(in+1)))>max)
{
max=a[3][3];
h=3;
k=3;
}
}while(s.length()<1);
int maxd=0;
if(h!=0&&k!=0&&h!=3&&k!=3)
{
maxd=a[h][k]-a[h][k-1];
if(maxd>a[h][k]-a[h][k+1])
maxd=a[h][k]-a[h][k+1];
if(maxd>a[h][k]-a[h+1][k])
maxd=a[h][k]-a[h+1][k];
if(maxd>a[h][k]-a[h-1][k])
maxd=a[h][k]-a[h-1][k];
}
else if(k==0||k==3)
{
if(h!=0)
maxd=a[h][k]-a[h-1][k];
else
maxd=a[h][k]-a[h+1][k];
if(maxd>a[h][k]-a[h+1][k])
maxd=a[h][k]-a[h+1][k];
if(k!=3)
if(maxd>a[h][k]-a[h][k+1])
maxd=a[h][k]-a[h][k+1];
if(k!=0)
if(maxd>a[h][k]-a[h][k-1])
maxd=a[h][k]-a[h][k-1];
}
else if(h==0||h==3)
{
if(k!=0)
maxd=a[h][k]-a[h][k-1];
else
maxd=a[h][k]-a[h][k+1];
if(maxd>a[h][k]-a[h][k+1])
maxd=a[h][k]-a[h][k+1];
if(h!=3)
if(maxd>a[h][k]-a[h+1][k])
maxd=a[h][k]-a[h+1][k];
if(h!=0)
if(maxd>a[h][k]-a[h-1][k])
maxd=a[h][k]-a[h-1][k];
}
System.out.println(maxd);
}
}
}