题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4801
题意:求旋转次数小于等于n(每次旋转90度)的情况下,求出魔方拼出完整的面的最大面数。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int ans;
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int dir[7][25]={
{0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23},//前左 或 后右
{0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23},//前右 或 后左
{1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8},//上右 或 下左
{2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4},//上左 或 下右
{6,1,12,3,5,11,16,7,8,9,4,10,18,13,14,15,20,17,22,19,0,21,2,23},//左上 或 右下
{20,1,22,3,10,4,0,7,8,9,11,5,2,13,14,15,6,17,12,19,16,21,18,23},//左下 或 右上
};
void dfs(int n,int MM[])
{
int num=0,i,j;
if(MM[0]==MM[1]&&MM[0]==MM[2]&&MM[0]==MM[3])
num++;
if(MM[4]==MM[5]&&MM[4]==MM[10]&&MM[4]==MM[11])
num++;
if(MM[6]==MM[7]&&MM[6]==MM[12]&&MM[6]==MM[13])
num++;
if(MM[8]==MM[9]&&MM[8]==MM[14]&&MM[8]==MM[15])
num++;
if(MM[16]==MM[17]&&MM[16]==MM[18]&&MM[16]==MM[19])
num++;
if(MM[20]==MM[21]&&MM[20]==MM[22]&&MM[20]==MM[23])
num++;
if(num>ans)
ans=num;
if(n==0||ans==6)
{
return ;
}
int M[25];
for(i=0;i<6;i++)
{
for(j=0;j<24;j++)
{
M[j]=MM[dir[i][j]];
}
dfs(n-1,M);
}
}
int main()
{
int n,i,j;
int MAP[25];
while(~scanf("%d",&n))
{
ans=0;
for(i=0;i<24;i++)
{
scanf("%d",&MAP[i]);
}
dfs(n,MAP);
printf("%d\n",ans);
}
return 0;
}