https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1402php
http://7xjob4.com1.z0.glb.clouddn.com/53f6b2526cc5a59ec7881a8fd6d899bdios
題意:將n個原有顏色的立方體塗儘可能少次使立方體都相同;c++
思路:枚舉除第一個面外每一個立方體的姿態(24種),姿態由旋轉方式先處理獲得,再枚舉每個對應面記錄要塗的數量。ide
處理代碼:spa
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int lleft[7]={0,2,6,3,4,1,5}; 5 int up[7]={0,4,2,1,6,5,3}; 6 7 void rot(int *T,int *p) 8 { 9 int q[7]; 10 for(int i=1;i<=6;i++) 11 { 12 q[i]=p[i]; 13 } 14 15 for(int i=1;i<=6;i++) 16 { 17 p[i]=q[T[i]]; 18 } 19 } 20 21 int main() 22 { 23 int p0[7]={0,1,2,3,4,5,6}; 24 25 printf("int dice[25][7]= {\n{0},\n"); 26 for(int i=1;i<=6;i++) 27 { 28 int p[7]; 29 for(int j=0;j<=6;j++) 30 p[j]=p0[j]; 31 32 if(i==1) rot(up,p); 33 if(i==2) { rot(lleft,p); rot(up,p); } 34 35 if(i==4) { rot(up,p);rot(up,p); } 36 if(i==5) { rot(lleft,p);rot(lleft,p);rot(lleft,p);rot(up,p); } 37 if(i==6) { rot(lleft,p);rot(lleft,p);rot(up,p); } 38 39 for(int j=1;j<=4;j++) 40 { 41 printf("{%d,%d,%d,%d,%d,%d,%d},",p[0],p[1],p[2],p[3],p[4],p[5],p[6]); 42 rot(lleft,p); 43 } 44 printf("\n"); 45 } 46 printf("}; \n"); 47 }
計算代碼:code
1 #include <bits/stdc++.h> 2 #include <iostream> 3 using namespace std; 4 5 int dice[25][7]= { 6 {0}, 7 {0,4,2,1,6,5,3},{0,2,3,1,6,4,5},{0,3,5,1,6,2,4},{0,5,4,1,6,3,2}, 8 {0,4,6,2,5,1,3},{0,6,3,2,5,4,1},{0,3,1,2,5,6,4},{0,1,4,2,5,3,6}, 9 {0,1,2,3,4,5,6},{0,2,6,3,4,1,5},{0,6,5,3,4,2,1},{0,5,1,3,4,6,2}, 10 {0,6,2,4,3,5,1},{0,2,1,4,3,6,5},{0,1,5,4,3,2,6},{0,5,6,4,3,1,2}, 11 {0,4,1,5,2,6,3},{0,1,3,5,2,4,6},{0,3,6,5,2,1,4},{0,6,4,5,2,3,1}, 12 {0,4,5,6,1,2,3},{0,5,3,6,1,4,2},{0,3,2,6,1,5,4},{0,2,4,6,1,3,5}, 13 }; 14 15 int n,ans; 16 int color[5][7],state[5]; 17 vector <string> colorname; 18 19 int colorid(char str[]) 20 { 21 int i,j; 22 string s(str); 23 int m=colorname.size(); 24 for(i=0;i<m;i++) 25 { 26 if(colorname[i]==s) 27 { 28 return i; 29 } 30 } 31 colorname.push_back(s); 32 return m; 33 } 34 35 void cal() 36 { 37 int i,j; 38 int num=0,maxnum; 39 map <int,int> cn; 40 for(j=1;j<=6;j++) 41 { 42 cn.clear(); 43 maxnum=0; 44 for(i=1;i<=n;i++) 45 { 46 int cnam=color[i][dice[state[i]][j]]; 47 cn[cnam]++; 48 if(cn[cnam]>maxnum) 49 maxnum=cn[cnam]; 50 } 51 num+=(n-maxnum); 52 } 53 if(ans>num) 54 ans=num; 55 } 56 57 void dfs(int m) 58 { 59 int i,j; 60 if(m==n) 61 { 62 cal(); 63 return; 64 } 65 for(i=1;i<=24;i++) 66 { 67 state[m+1]=i; 68 dfs(m+1); 69 } 70 } 71 int main() 72 { 73 char str[30]; 74 int i,j; 75 while(scanf("%d",&n)!=EOF && n!=0) 76 { 77 colorname.clear(); 78 for(i=1;i<=n;i++) 79 { 80 for(j=1;j<=6;j++) 81 { 82 scanf("%s",str); 83 color[i][j]=colorid(str); 84 } 85 } 86 87 if(n==1) 88 { 89 ans=0; 90 } 91 else 92 { 93 ans=24; 94 state[1]=9; 95 dfs(1); 96 } 97 98 printf("%d\n",ans); 99 } 100 return 0; 101 } 102 103 /* 104 for(i=1;i<=n;i++) 105 { 106 for(j=1;j<=6;j++) 107 { 108 printf("%s ",color[i][j]); 109 } 110 printf("\n"); 111 } 112 if(strcmp(color[1][2],color[2][3])==0) 113 { 114 printf("yes\n"); 115 } 116 */