TOJ-1314 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.

Input

The first line of the input file 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.

Output

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

Sample Input

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

Sample Output

29

Hint

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



Source: Tehran 2004 Iran Nationwide

由于相邻两个筛子接触面点数相同,那么第一个筛子的上面确定后,所有筛子的摆法也就确定了。

#include <iostream>
using namespace std;
int dice[10010][7];
int op(int i){
    switch(i){
        case 1:return 6;
        case 2:return 4;
        case 3:return 5;
        case 4:return 2;
        case 5:return 3;
        case 6:return 1;
    }
}
int sidemax(int d,int u){
    int max = 0;
    for(int i=1;i<=6;i++){
        if(i!=u && i!=op(u)){
            if(max<dice[d][i])
                max = dice[d][i];
        }
    }
    return max;
}
int main(){
    int t,n,i,j,k,top,bot,max,sum;
    cin>>t;
    while(t--){
        cin>>n;
        for(i=1;i<=n;i++){
            cin>>dice[i][1]>>dice[i][2]>>dice[i][3]>>dice[i][4]>>dice[i][5]>>dice[i][6];
        }
        max = 0;
        for(i=1;i<=6;i++){
            sum = 0;
            top = dice[1][i];
            bot = dice[1][op(i)];
            sum += sidemax(1,i);
            for(j=2;j<=n;j++){
                for(k=1;k<=6;k++)
                    if(dice[j][k]==bot){
                        top = bot;
                        bot = dice[j][op(k)];
                        break;
                    }
                sum += sidemax(j,k);
            }
            if(max<sum)
                max = sum;
        }
        cout<<max<<endl;
    }
    return 0;
}

 

posted @ 2017-01-22 20:18  DGSX  阅读(169)  评论(0编辑  收藏  举报