遇到一个问题如下
正六面体染色
正六面体用4种颜色染色。
共有多少种不同的染色样式?
要考虑六面体可以任意旋转、翻转。
算法如下:
1 #include <stdio.h> 2 #pragma warning(disable: 4786) 3 #include <set> 4 5 using namespace std; 6 7 void print(int arr[], int total_index) 8 { 9 const char *szColors[] = {"红", "黄", "蓝", "绿", "白", "黑", "紫", "灰", "粉"}; 10 const char *szSides[] = {"前", "右", "后", "左", "上", "下"}; 11 int index; 12 13 printf("%d: ", total_index); 14 15 for (index = 0; index < 6; index += 1) 16 { 17 printf("%s:%s ", szSides[index], szColors[arr[index]]); 18 } 19 20 printf("\n"); 21 } 22 23 void resolve(int nColorCount) 24 { 25 set<int> setHashs; 26 int arr[6 * 2]; 27 int index, index2, index3 = 0; 28 29 for (arr[0] = 0; arr[0] < nColorCount; arr[0] += 1) 30 for (arr[1] = 0; arr[1] < nColorCount; arr[1] += 1) 31 for (arr[2] = 0; arr[2] < nColorCount; arr[2] += 1) 32 for (arr[3] = 0; arr[3] < nColorCount; arr[3] += 1) 33 for (arr[4] = 0; arr[4] < nColorCount; arr[4] += 1) 34 for (arr[5] = 0; arr[5] < nColorCount; arr[5] += 1) 35 { 36 int hash_big = 0; 37 38 for (index = 0; index < 6; index += 1) 39 { 40 arr[index + 6] = arr[index]; 41 } 42 43 for (index = 0; index < 6; index += 1) 44 { 45 int hash_this = 0; 46 47 for (index2 = 0; index2 < 6; index2 += 1) 48 { 49 hash_this *= 10; 50 hash_this += arr[index + index2]; 51 } 52 53 if (hash_this > hash_big) 54 { 55 hash_big = hash_this; 56 } 57 } 58 59 if (setHashs.find(hash_big) == setHashs.end()) 60 { 61 setHashs.insert(hash_big); 62 print(arr, ++index3); 63 } 64 } 65 } 66 67 int main() 68 { 69 resolve(4); 70 return 0; 71 }
答案是700种方案