题目传送门

题意:魔方最多旋转n次,问最多能使多少面颜色相同

分析:6种状态(3种旋转方式*顺逆方向,其他对称的!),首先先打个表,然后很愉快的DFS。自己写的时候费劲脑汁,代码很长,还TLE了。。。。

 

/************************************************
* Author        :Running_Time
* Created Time  :2015/10/24 星期六 14:18:27
* File Name     :K.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 24;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
int ans;
int B[6][24]={ {6,1,12,3,5,11,16,7,8,9,4,10,18,13,14,15,20,17,22,19,0,21,2,23}, //ok
               {20,1,22,3,10,4,0,7,8,9,11,5,2,13,14,15,6,17,12,19,16,21,18,23}, //ok
               {1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8}, //ok
               {2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4}, //ok
               {0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23}, //ok
               {0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23}  //ok
              };
int cal(int *a)   {
    int ret = 0;
    if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3])   ret++;
    if (a[6] == a[7] && a[7] == a[12] && a[12] == a[13])    ret++;
    if (a[4] == a[5] && a[5] == a[10] && a[10] == a[11])    ret++;
    if (a[8] == a[9] && a[9] == a[14] && a[14] == a[15])    ret++;
    if (a[16] == a[17] && a[17] == a[18] && a[18] == a[19]) ret++;
    if (a[20] == a[21] && a[21] == a[22] && a[22] == a[23]) ret++;
    return ret;
}

void DFS(int tot, int *h)  {
    ans = max (ans, cal (h));
    if (!tot) {
        return ;
    }
    int p[24];
    for (int i=0; i<6; ++i) {
        for (int j=0; j<24; ++j)    p[j] = h[B[i][j]];
        DFS (tot - 1, p);
    }
    return ;
}

int main(void)    {
    int n;  int a[24];
    while (scanf ("%d", &n) == 1)   {
        for (int i=0; i<24; ++i)    scanf ("%d", &a[i]);
        ans = 0;
        DFS (n, a);
        printf ("%d\n", ans);
    }

    return 0;
}

  

编译人生,运行世界!