CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » Technovanza » Board

Board

Problem code: TECH13

  • All Submissions

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 :

  1. The first player can pick any number from the board, this is added to his personal total.
  2. 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.
  3. 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


  • Submit

Comments

  • Login or Register to post a comment.

class Main{ public static

dwatson_bynes @ 28 Jan 2010 09:31 PM

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);
}
}
}

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com