jzoj3903 立方體

Description

這裏寫圖片描述

Input

這裏寫圖片描述

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
0

2
red green gray gray magenta cyan
cyan green gray gray magenta red
3
red green blue yellow magenta cyan
cyan green blue yellow magenta red
magenta red blue yellow cyan green
0

Sample Output

4
2

0
2

analysis

這是一道搜索題(再次暴露了我搜索技巧的極度不成熟)
一開始寫的是枚舉哪些面該被重新染色,結果死活調不出來。
其實只需要枚舉正方體的放置方法
然後一比對就好了
要用哈希來搞一搞顏色。

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
char s[500],s1;
int n,i,l,j,ans,h,k,t,cnt,d[50],q[150][3];
ll a[5][10];
const int b[24][6]=
    {{1,2,3,4,5,6},
        {4,2,1,6,5,3},
        {6,2,4,3,5,1},
        {3,2,6,1,5,4},
        {5,3,6,1,4,2},
        {1,3,5,2,4,6},
        {2,3,1,6,4,5},
        {6,3,2,5,4,1},
        {4,6,2,5,1,3},
        {5,6,4,3,1,2},
        {3,6,5,2,1,4},
        {2,6,3,4,1,5},
        {1,5,4,3,2,6},
        {3,5,1,6,2,4},
        {6,5,3,4,2,1},
        {4,5,6,1,2,3},
        {2,4,6,1,3,5},
        {1,4,2,5,3,6},
        {5,4,1,6,3,2},
        {6,4,5,2,3,1},
        {3,1,2,5,6,4},
        {5,1,3,4,6,2},
        {4,1,5,2,6,3},
        {2,1,4,3,6,5}};
const int mo1=1000000007;
const int mo2=1000000003;
const int mo=97;
void dfs(int x){
   int i,j,k;
   if (x<=n) {
     fo(i,1,24) {
       d[x]=i;
       dfs(x+1);}
   } else {
    int t,s;
    t=0; 
    fo(i,0,5) {
     s=0;
      fo(j,1,n-1) {
       h=0;
        fo(k,j+1,n) {
            if (a[j][b[d[j]-1][i]]==a[k][b[d[k]-1][i]]) h++;
           }
        s=max(s,h); 
     }
     t+=(n-s-1);
    }
    ans=min(ans,t);
   }
}
int get(ll x,ll y){
    int t=x&mo;
    while (!(q[t][1]==-1||(q[t][1]==x&q[t][2]==y))) t=(t+1)%mo;
    if (q[t][1]==-1)
      q[t][1]=x,q[t][2]=y;
    return t;
}
ll hash(){
    int i; ll h1,h2;
    h1=0; h2=0;
    fo(i,1,l) {
      h1=(h1*26-'a'+1+s[i])%mo1;
      h2=(h2*26-'a'+1+s[i])%mo2;}
    return get(h1,h2);
}
int main(){
//  freopen("1.in","r",stdin);
//  freopen("1.out","w",stdout);
    scanf("%d",&n);
    while (n){
        memset(q,255,sizeof(q));
        memset(d,0,sizeof(d));
        fo(i,1,n){
            fo(h,1,6){
            scanf("%s",s+1);
            l=strlen(s+1);
            a[i][h]=hash();
            }}
        ans=10000000;
        d[1]=1;
        dfs(1);
        printf("%d\n",ans);
        scanf("%d",&n);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章