正六面体用若干种颜色染色的问题解法

注意:本文介绍的方案有误,正确答案为240种。http://books.google.com.hk/books?id=On5QwCvEA9kC&pg=PA105&lpg=PA105&dq=%E5%85%AD%E9%9D%A2%E4%BD%93+%E5%9B%9B%E7%A7%8D%E9%A2%9C%E8%89%B2&source=bl&ots=Lc7xqJW4IC&sig=uG7Llh_kDgZSN3BGhLWaBU0nANg&hl=en&sa=X&ei=NYtXUYHFIY-UiQfhnYDQCQ&ved=0CEsQ6AEwAw#v=onepage&q&f=false

 

遇到一个问题如下

正六面体染色
正六面体用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种方案

posted on 2013-03-30 21:05  Shilyx  阅读(1204)  评论(0编辑  收藏  举报

导航