首页 > 专题系列 > Java解POJ > POJ 1972 Dice Stacking [解题报告] Java
2013
11-10

POJ 1972 Dice Stacking [解题报告] Java

Dice Stacking

问题描述 :

Chun-Soo is playing a dice stacking game. Six faces of a die are squares of the same size; each face of a die has a number from 1 to 6. But, they are not standard dice because the sum of the numbers in the opposite faces may not always be 7.

The dice game is to stack several dice up in the order Die 1, Die 2, Die 3, from the base keeping the following rule: For each pair of consecutive dice, the number on the bottom of the top die must match the number on the top of the bottom die. In other words, the two faces that are together must have the same number. We are free to place the bottom die as we wish, so Die 1 can be set freely.

A long rectangular pillar which has 4 sides is created. We are trying to make one side have the maximum sum. (Note that after we rotate a die to fix the top face and bottom face, we can still rotate the die by 90, 180, or 270 degrees.) Write a program to find the maximum sum that one side can have.

输入:

The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The first line for each test case contains an integer n (1 <= n <= 10,000), the number of dice. In the next lines, each line contains six integers for a die, by the order A, B, C, D, E, F, as in the following figure. There is a single space between two numbers. The number of dice is less than or equal to 10,000. It is possible for two dice to be the same.

输出:

There should be one line per test case which contains the maximum sum of the numbers in one side.

样例输入:

1
5
2 3 1 6 5 4
3 1 2 4 6 5
5 6 4 1 3 2
1 3 6 2 4 5
4 1 6 5 2 3

样例输出:

29

温馨提示:

To get the maximum sum for the above input, you should pile up the dice as follows:

解题代码:

//* @author: 
import java.util.*;
public class Main
{
 static int max(int u,int v,int w,int x)
{
	if (u< v)u=v;
	if (u< w)u=w;
	if (u< x)u=x;
	return u;
}

 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   
   int a[][]=new int[2][7],s[]=new int[6];
       int nn=sc.nextInt();
	while ((nn--)!=0)
	{
	 int n=sc.nextInt();
        for(int i=0;i< s.length;i++)
          s[i]=sc.nextInt();
	 a[0][s[0]]=a[0][s[5]]=max(s[1],s[2],s[3],s[4]);
	 a[0][s[1]]=a[0][s[3]]=max(s[0],s[2],s[4],s[5]);
	 a[0][s[2]]=a[0][s[4]]=max(s[0],s[1],s[3],s[5]);
	 for(int i=1;i< n;i++)
	 {
	  for(int j=0;j< s.length;j++)
            s[j]=sc.nextInt();

	  a[i&1][s[0]]=a[(i+1)&1][s[5]]+max(s[1],s[2],s[3],s[4]);
	  a[i&1][s[5]]=a[(i+1)&1][s[0]]+max(s[1],s[2],s[3],s[4]);
	  a[i&1][s[1]]=a[(i+1)&1][s[3]]+max(s[0],s[2],s[4],s[5]);
	  a[i&1][s[3]]=a[(i+1)&1][s[1]]+max(s[0],s[2],s[4],s[5]);
	  a[i&1][s[2]]=a[(i+1)&1][s[4]]+max(s[0],s[1],s[3],s[5]);
	  a[i&1][s[4]]=a[(i+1)&1][s[2]]+max(s[0],s[1],s[3],s[5]);
	}
	int ans=0;
	if ((n%2)==0) for (int i=1;i<=6;i++) if (ans< a[1][i]) ans=a[1][i];
	if (n%2!=0) for (int i=1;i<=6;i++) if (ans< a[0][i]) ans=a[0][i];
	System.out.printf("%d\n",ans);
     }
  }
}