TOJ-1314 Dice Stacking

本文介绍了一种解决骰子堆叠游戏问题的算法,目标是通过特定规则堆叠骰子,使一侧的数字总和达到最大。文章详细解释了输入输出格式,给出了示例,并提供了实现该算法的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/shenchuguimo/p/6341335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值