FOJ 1174 Dice Stacking

Dice Stacking
Time Limit:1s Memory limit:32M
Accepted Submit:27 Total Submit:36

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 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:

Original: Iran 2004

模拟.....................一次AC,RP爆发的说

又是一个很好玩的题目

  1. #include <stdio.h>
  2. typedef struct dice
  3. {
  4.     int num[6];
  5. }Dice[10000];
  6. int max_num(dice *d,int i,int index)
  7. {int mn=0;
  8.     if(index==0||index==5) 
  9.         {
  10.         if(d[i].num[1]>mn) mn=d[i].num[1];
  11.         if(d[i].num[2]>mn) mn=d[i].num[2];
  12.         if(d[i].num[3]>mn) mn=d[i].num[3];
  13.         if(d[i].num[4]>mn) mn=d[i].num[4];
  14.        return mn;
  15.         }
  16.     if(index==1||index==3) 
  17.         {
  18.         if(d[i].num[0]>mn) mn=d[i].num[0];
  19.         if(d[i].num[2]>mn) mn=d[i].num[2];
  20.         if(d[i].num[5]>mn) mn=d[i].num[5];
  21.         if(d[i].num[4]>mn) mn=d[i].num[4];
  22.        return mn;
  23.         }
  24.     if(index==2||index==4) 
  25.         {
  26.         if(d[i].num[1]>mn) mn=d[i].num[1];
  27.         if(d[i].num[0]>mn) mn=d[i].num[0];
  28.         if(d[i].num[3]>mn) mn=d[i].num[3];
  29.         if(d[i].num[5]>mn) mn=d[i].num[5];
  30.        return mn;
  31.         }
  32. }
  33. int find_fit_index(dice *d,int i,int val)
  34. {
  35.     int j;
  36.     for(j=0;j<6;j++)
  37.         if(d[i].num[j]==val) return j;
  38. }
  39. int val_next(dice *d,int i,int num_index)
  40. {
  41.  if(num_index==0) return d[i].num[5];
  42.  if(num_index==5) return d[i].num[0];
  43.  if(num_index==1) return d[i].num[3];
  44.  if(num_index==3) return d[i].num[1];
  45.  if(num_index==2) return d[i].num[4];
  46.  if(num_index==4) return d[i].num[2];
  47. }
  48. int main()
  49. {
  50.  Dice D;
  51.  int n,t,i,msum,sum,j,val;
  52.  scanf("%d",&t);
  53.  while(t--)
  54.     {
  55.      scanf("%d",&n);
  56.      msum=0;
  57.      for(i=0;i<n;i++)
  58.          scanf("%d%d%d%d%d%d",&D[i].num[0],&D[i].num[1],&D[i].num[2],&D[i].num[3],&D[i].num[4],&D[i].num[5]);
  59.      for(i=0;i<6;i++)
  60.         {
  61.             sum=max_num(D,0,i);
  62.             val=D[0].num[i];
  63.             for(j=1;j<n;j++)
  64.             {
  65.                 sum+=max_num(D,j,find_fit_index(D,j,val));      
  66.                 val=val_next(D,j,find_fit_index(D,j,val));
  67.             }
  68.             msum=msum>sum?msum:sum;
  69.         }
  70.      printf("%d/n",msum);
  71.     }
  72.  return 0;
  73. }

 

你可能感兴趣的:(FOJ 1174 Dice Stacking)