Description
There are several colored cubes. All of them are of the same size but they may be colored differently. Each face of these cubes has a single color. Colors of distinct faces of a cube may or may not be the same.
Two cubes are said to be identically colored if some suitable
rotations of one of the cubes give identical looks to both of the cubes.
For example, two cubes shown in Figure 2 are identically colored. A set
of cubes is said to be identically colored if every pair of them are
identically colored.
A cube and its mirror image are not necessarily identically colored.
For example, two cubes shown in Figure 3 are not identically colored.
You can make a given set of cubes identically colored by repainting
some of the faces, whatever colors the faces may have. In Figure 4,
repainting four faces makes the three cubes identically colored and
repainting fewer faces will never do.
Your task is to write a program to calculate the minimum number of
faces that needs to be repainted for a given set of cubes to become
identically colored.
Input
The
input is a sequence of datasets. A dataset consists of a header and a
body appearing in this order. A header is a line containing one positive
integer n and the body following it consists of n lines. You can assume
that 1 <= n <= 4. Each line in a body contains six color names
separated by a space. A color name consists of a word or words connected
with a hyphen (-). A word consists of one or more lowercase letters.
You can assume that a color name is at most 24-characters long including
hyphens.
A dataset corresponds to a set of colored cubes. The integer n
corresponds to the number of cubes. Each line of the body corresponds to
a cube and describes the colors of its faces. Color names in a line is
ordered in accordance with the numbering of faces shown in Figure 5. A
line
- color1 color2 color3 color4 color5 color6
corresponds to a cube colored as shown in Figure 6.
The end of the input is indicated by a line containing a single zero. It is not a dataset nor a part of a dataset.
Output
For
each dataset, output a line containing the minimum number of faces that
need to be repainted to make the set of cubes identically colored.
Sample Input
3 scarlet green blue yellow magenta cyan blue pink green magenta cyan lemon purple red blue yellow cyan green 2 red green blue yellow magenta cyan cyan green blue yellow magenta red 2 red green gray gray magenta cyan cyan green gray gray magenta red 2 red green blue yellow magenta cyan magenta red blue yellow cyan green 3 red green blue yellow magenta cyan cyan green blue yellow magenta red magenta red blue yellow cyan green 3 blue green green green green blue green blue blue green green green green green green green green sea-green 3 red yellow red yellow red yellow red red yellow yellow red yellow red red red red red red 4 violet violet salmon salmon salmon salmon violet salmon salmon salmon salmon violet violet violet salmon salmon violet violet violet violet violet violet salmon salmon 1 red green blue yellow magenta cyan 4 magenta pink red scarlet vermilion wine-red aquamarine blue cyan indigo sky-blue turquoise-blue blond cream chrome-yellow lemon olive yellow chrome-green emerald-green green olive vilidian sky-blue 0
Sample Output
4 2 0 0 2 3 4 4 0 16
正解:搜索
解题报告:
今天考试考了这道题,考场上调试了一会儿,毕竟是半码农题。。。
考虑对于每一个立方体,如果我确立了一个面为顶面,则还可选择相邻的四个面中的一个,则可唯一确定一个立方体,那么一共有6*4=24种状态。一共有4个立方体,那么显然我只需要保持第一个不动,剩下三个枚举哪种状态就可以了。然后我们再对于四个确立好状态的立方体每个面贪心地染色,可以得出答案。
对于题目给的颜色名称我直接用map映射到int上去了,然后可以直接编号。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #ifdef WIN32 14 #define OT "%I64d" 15 #else 16 #define OT "%lld" 17 #endif 18 using namespace std; 19 typedef long long LL; 20 const int MAXN = 70; 21 int biao[250][60]={ 22 {0,1,2,3,4,5},{0,2,4,1,3,5},{0,4,3,2,1,5},{0,3,1,4,2,5}, 23 {1,2,0,5,3,4},{1,5,2,3,0,4},{1,0,3,2,5,4},{1,3,5,0,2,4}, 24 {2,1,5,0,4,3},{2,0,1,4,5,3},{2,4,0,5,1,3},{2,5,4,1,0,3}, 25 {3,4,5,0,1,2},{3,5,1,4,0,2},{3,1,0,5,4,2},{3,0,4,1,5,2}, 26 {4,0,2,3,5,1},{4,2,5,0,3,1},{4,5,3,2,0,1},{4,3,0,5,2,1}, 27 {5,2,1,4,3,0},{5,1,3,2,4,0},{5,4,2,3,1,0},{5,3,4,1,2,0}, 28 }; 29 int n; 30 int paint[MAXN][60]; 31 int ans,ecnt; 32 int rotat[MAXN],color[MAXN][60]; 33 string ch; 34 int col_cnt[MAXN*6];//每种颜色 35 map<string,int>mp; 36 37 inline int getint() 38 { 39 int w=0,q=0; 40 char c=getchar(); 41 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); 42 if (c==‘-‘) q=1, c=getchar(); 43 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); 44 return q ? -w : w; 45 } 46 47 inline void dfs(int d){ 48 if(d==n){ 49 for(int i=0;i<n;i++) for(int j=0;j<6;j++) color[i][ biao[ rotat[i] ][j] ]=paint[i][j]; 50 51 int tot=0; 52 for(int j=0;j<6;j++) {//枚举每个面 53 memset(col_cnt,0,sizeof(col_cnt)); 54 int now=0; 55 for(int i=0;i<n;i++){//考虑每个立方体 56 col_cnt[ color[i][j] ]++; 57 now=max(now,col_cnt[color[i][j]]); 58 } 59 tot+=n-now; 60 } 61 ans=min(ans,tot); 62 63 return ; 64 } 65 for(int i=0;i<24;i++) rotat[d]=i,dfs(d+1); 66 } 67 68 inline void work(){ 69 while(1) { 70 n=getint(); if(n==0) break; 71 for(int i=0;i<n;i++) 72 for(int j=0;j<6;j++) { 73 cin>>ch; 74 if(mp[ch]!=0) paint[i][j]=mp[ch]; 75 else { mp[ch]=++ecnt; paint[i][j]=mp[ch]; } 76 } 77 ans=n*6;rotat[0]=0;//第一个立方体固定不动 78 dfs(1); printf("%d\n",ans); 79 } 80 } 81 82 int main() 83 { 84 work(); 85 return 0; 86 }