zoukankan      html  css  js  c++  java
  • hdu5836, 2016CCPC网络赛

    题目大意:给你一个纯白色的魔方,给其中n<=5个格子涂上黑色,然后随便扭,问有多少种可达状态(去掉旋转后可得到的重复情况)

    傻逼一样的题目,暴力直接做就行,赛场上想去重时想乱了。其实给魔方加上一个旋转中间层就好处理了。我这里贴一下代码当做是模板了。

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <cmath>
      6 #include <queue>
      7 
      8 using namespace std;
      9 
     10 #define LL long long
     11 #define MAXN 54
     12 #define hash MY_HASH
     13 #define MOD 999997
     14 #define FMZ 0
     15 #define BMZ 5
     16 #define UMZ 2
     17 #define DMZ 3
     18 #define LMZ 4
     19 #define RMZ 1
     20 
     21 int a[MAXN], ans, n;
     22 queue <LL > q;
     23 vector <LL > hash[MOD];
     24 
     25 struct MagicCube{
     26     typedef char Elem;
     27     #define f c[0]
     28     #define b c[5]
     29     #define u c[2]
     30     #define d c[3]
     31     #define l c[4]
     32     #define r c[1]
     33 
     34 //    Elem f[9], b[9], u[9], d[9], l[9], r[9], tmp[9], ch;
     35     Elem ch, tmp[9], c[6][9];
     36 
     37     void init() {
     38         for (int i = 0; i < 9; ++i) u[i] = a[i];
     39         for (int i = 0; i < 9; ++i) l[i] = a[i+9];
     40         for (int i = 0; i < 9; ++i) f[i] = a[i+18];
     41         for (int i = 0; i < 9; ++i) r[i] = a[i+27];
     42         for (int i = 0; i < 9; ++i) b[i] = a[i+36];
     43         for (int i = 0; i < 9; ++i) d[i] = a[i+45];
     44     }
     45     void decode(LL code) {
     46         for (int i = 0; i < 9; ++i) {u[i] = code & 1; code >>= 1;}
     47         for (int i = 0; i < 9; ++i) {l[i] = code & 1; code >>= 1;}
     48         for (int i = 0; i < 9; ++i) {f[i] = code & 1; code >>= 1;}
     49         for (int i = 0; i < 9; ++i) {r[i] = code & 1; code >>= 1;}
     50         for (int i = 0; i < 9; ++i) {b[i] = code & 1; code >>= 1;}
     51         for (int i = 0; i < 9; ++i) {d[i] = code & 1; code >>= 1;}
     52     }
     53 
     54     LL encode() {
     55         LL res = 0;
     56         for (int i = 0; i < 9; ++i) if (u[i]) res = res + (1LL << (i + 0));
     57         for (int i = 0; i < 9; ++i) if (l[i]) res = res + (1LL << (i + 9));
     58         for (int i = 0; i < 9; ++i) if (f[i]) res = res + (1LL << (i + 18));
     59         for (int i = 0; i < 9; ++i) if (r[i]) res = res + (1LL << (i + 27));
     60         for (int i = 0; i < 9; ++i) if (b[i]) res = res + (1LL << (i + 36));
     61         for (int i = 0; i < 9; ++i) if (d[i]) res = res + (1LL << (i + 45));
     62         return res;
     63     }
     64 
     65     int right_rotate(Elem a[9]) {
     66         for (int i = 0; i < 9; ++i) tmp[i] = a[i];
     67         for (int i = 0; i < 3; ++i)
     68             for (int j = 0; j < 3; ++j) 
     69                 a[i*3+j] = tmp[3*(2 - j) + i];
     70     }
     71 
     72     void L(int cnt = 1) {
     73         for (; cnt>0; --cnt) {
     74             right_rotate(l);
     75             ch=u[0];u[0]=b[8];b[8]=d[0];d[0]=f[0];f[0]=ch;
     76             ch=u[3];u[3]=b[5];b[5]=d[3];d[3]=f[3];f[3]=ch;
     77             ch=u[6];u[6]=b[2];b[2]=d[6];d[6]=f[6];f[6]=ch;
     78         }
     79     }
     80     void R(int cnt = 1) {
     81         for (; cnt>0; --cnt) {
     82             right_rotate(r);
     83             ch=u[2];u[2]=f[2];f[2]=d[2];d[2]=b[6];b[6]=ch;
     84             ch=u[5];u[5]=f[5];f[5]=d[5];d[5]=b[3];b[3]=ch;
     85             ch=u[8];u[8]=f[8];f[8]=d[8];d[8]=b[0];b[0]=ch;
     86         }
     87     }
     88     void U(int cnt = 1) {
     89         for (; cnt>0; --cnt) {
     90             right_rotate(u);
     91             ch=f[0];f[0]=r[0];r[0]=b[0];b[0]=l[0];l[0]=ch;
     92             ch=f[1];f[1]=r[1];r[1]=b[1];b[1]=l[1];l[1]=ch;
     93             ch=f[2];f[2]=r[2];r[2]=b[2];b[2]=l[2];l[2]=ch;    
     94         }
     95     }
     96     void D(int cnt = 1) {
     97         for (; cnt>0; --cnt) {
     98             right_rotate(d);
     99             ch=f[6];f[6]=l[6];l[6]=b[6];b[6]=r[6];r[6]=ch;
    100             ch=f[7];f[7]=l[7];l[7]=b[7];b[7]=r[7];r[7]=ch;
    101             ch=f[8];f[8]=l[8];l[8]=b[8];b[8]=r[8];r[8]=ch;
    102         }
    103     }
    104     void F(int cnt = 1) {
    105         for (; cnt > 0; --cnt) {
    106             right_rotate(f);
    107             ch=u[6];u[6]=l[8];l[8]=d[2];d[2]=r[0];r[0]=ch;
    108             ch=u[7];u[7]=l[5];l[5]=d[1];d[1]=r[3];r[3]=ch;
    109             ch=u[8];u[8]=l[2];l[2]=d[0];d[0]=r[6];r[6]=ch;
    110         }
    111     }
    112     void B(int cnt = 1) {
    113         for (; cnt > 0; --cnt) {
    114             right_rotate(b);
    115             ch=u[0];u[0]=r[2];r[2]=d[8];d[8]=l[6];l[6]=ch;
    116             ch=u[1];u[1]=r[5];r[5]=d[7];d[7]=l[3];l[3]=ch;
    117             ch=u[2];u[2]=r[8];r[8]=d[6];d[6]=l[0];l[0]=ch;
    118         }
    119     }
    120     void MI() {
    121         //ch=f[3],f[3]=l[5],l[5]=l[4],l[4]=l[3],l[3]=b[5],b[5]=b[4],b[4]=b[3],b[3]=r[5],r[5]=r[4],r[4]=r[3],r[3]=f[5],f[5]=f[4],f[4]=ch;
    122         ch=f[3]; f[3]=r[3]; r[3]=b[3]; b[3]=l[3]; l[3]=ch;
    123         ch=f[4]; f[4]=r[4]; r[4]=b[4]; b[4]=l[4]; l[4]=ch;
    124         ch=f[5]; f[5]=r[5]; r[5]=b[5]; b[5]=l[5]; l[5]=ch;
    125     //follow U
    126     }
    127     void MJ() {
    128 //        ch=u[5],u[5]=r[1],r[1]=r[4],r[4]=r[7],r[7]=d[5],d[5]=d[4],d[4]=d[3],d[3]=l[7],l[7]=l[4],l[4]=l[1],l[1]=u[3],u[3]=u[4],u[4]=ch;
    129         ch=u[3]; u[3]=l[7]; l[7]=d[5]; d[5]=r[1]; r[1]=ch;
    130         ch=u[4]; u[4]=l[4]; l[4]=d[4]; d[4]=r[4]; r[4]=ch;
    131         ch=u[5]; u[5]=l[1]; l[1]=d[3]; d[3]=r[7]; r[7]=ch;
    132     //follow F
    133     }
    134     void MK() {
    135 //        ch=u[1],u[1]=u[4],u[4]=u[7],u[7]=f[1],f[1]=f[4],f[4]=f[7],f[7]=d[1],d[1]=d[4],d[4]=d[7],d[7]=b[7],b[7]=b[4],b[4]=b[1],b[1]=ch;
    136 
    137         ch=u[1]; u[1]=b[7]; b[7]=d[1]; d[1]=f[1]; f[1]=ch;
    138         ch=u[4]; u[4]=b[4]; b[4]=d[4]; d[4]=f[4]; f[4]=ch;
    139         ch=u[7]; u[7]=b[1]; b[1]=d[7]; d[7]=f[7]; f[7]=ch;
    140     //follow L
    141     }
    142     void print() {
    143         for (int i = 0; i < 9; ++i) cout << (int)f[i]; cout << endl;
    144         for (int i = 0; i < 9; ++i) cout << (int)d[i]; cout << endl;
    145         for (int i = 0; i < 9; ++i) cout << (int)l[i]; cout << endl;
    146         for (int i = 0; i < 9; ++i) cout << (int)r[i]; cout << endl;
    147         for (int i = 0; i < 9; ++i) cout << (int)u[i]; cout << endl;
    148         for (int i = 0; i < 9; ++i) cout << (int)b[i]; cout << endl;
    149     }
    150     #undef f 
    151     #undef b
    152     #undef u
    153     #undef d
    154     #undef l
    155     #undef r
    156 
    157 } mc;
    158 
    159 inline void addHash(const LL &x) {
    160     hash[x%MOD].push_back(x);    
    161 }
    162 
    163 inline bool inHash(const LL &x) {
    164     int t = x % MOD;
    165     for (int i = 0; i <    hash[t].size(); ++i) {
    166         if (hash[t][i] == x) return true;
    167     }
    168     return false;
    169 }
    170 
    171 inline void rotateI(MagicCube &t) {
    172     t.U();
    173     t.MI();
    174     t.D(3);
    175 }
    176 
    177 inline void rotateJ(MagicCube &t) {
    178     t.L();
    179     t.MK();
    180     t.R(3);
    181 }
    182 
    183 inline void rotateK(MagicCube &t) {
    184     t.F();
    185     t.MJ();
    186     t.B(3);
    187 }
    188 
    189 inline bool check(const MagicCube &tmp) {
    190     MagicCube t = tmp;    
    191     /*
    192 
    193     for (int i = 0; i < 4; ++i) {
    194         for (int j = 0; j < 4; ++j) {
    195             for (int k = 0; k < 4; ++k) {
    196                 if (inHash(t.encode())) return false;
    197                 rotateK(t);
    198             }
    199             rotateJ(t);
    200         }
    201         rotateI(t);
    202     }
    203     return true;
    204 */
    205     for (int j = 0; j < 4; ++j) {    
    206         for (int i = 0; i < 4; ++i) {
    207             if (inHash(t.encode())) return false;
    208             rotateI(t);
    209         }
    210         rotateK(t);
    211     }
    212     rotateJ(t);
    213     for (int i = 0; i < 4; ++i) {
    214         if (inHash(t.encode())) return false;
    215         rotateI(t);
    216     }
    217     rotateJ(t); rotateJ(t);
    218     for (int i = 0; i < 4; ++i) {
    219         if (inHash(t.encode())) return false;
    220         rotateI(t);
    221     }
    222 
    223     return true;
    224 }
    225 
    226 
    227 void solve() {
    228     ans = 0;
    229     while (q.size()) q.pop();
    230     LL u = mc.encode();
    231     for (int i = 0; i < MOD; ++i) hash[i].clear(); 
    232     addHash(u); q.push(u);
    233     MagicCube tmp;
    234     LL key;
    235     while (q.size()) {
    236         u = q.front(); ++ans; q.pop();
    237         tmp.decode(u);
    238         for (int i = 1; i <= 4; ++i) {
    239             tmp.L();
    240             if (i == 4) break;
    241             if (check(tmp)) {
    242             key = tmp.encode();    
    243                 addHash(key);
    244                 q.push(key);
    245             }
    246         }
    247         for (int i = 1; i <= 4; ++i) {
    248             tmp.F();
    249             if (i == 4) break;
    250             if (check(tmp)) {
    251             key = tmp.encode();    
    252                 addHash(key);
    253                 q.push(key);
    254             }
    255         }
    256         for (int i = 1; i <= 4; ++i) {
    257             tmp.U();
    258             if (i == 4) break;
    259             if (check(tmp)) {
    260             key = tmp.encode();    
    261                 addHash(key);
    262                 q.push(key);
    263             }
    264         }
    265         for (int i = 1; i <= 4; ++i) {
    266             tmp.D();
    267             if (i == 4) break;
    268             if (check(tmp)) {
    269             key = tmp.encode();    
    270                 addHash(key);
    271                 q.push(key);
    272             }
    273         }
    274         for (int i = 1; i <= 4; ++i) {
    275             tmp.R();
    276             if (i == 4) break;
    277             if (check(tmp)) {
    278             key = tmp.encode();    
    279                 addHash(key);
    280                 q.push(key);
    281             }
    282         }
    283         for (int i = 1; i <= 4; ++i) {
    284             tmp.B();
    285             if (i == 4) break;
    286             if (check(tmp)) {
    287             key = tmp.encode();    
    288                 addHash(key);
    289                 q.push(key);
    290             }
    291         }
    292     }
    293 }
    294 
    295 int main() {
    296 
    297     int cas; scanf("%d", &cas);
    298     for (int tt = 1; tt <= cas; ++tt) {
    299         memset(a, 0, sizeof(a));
    300         scanf("%d", &n);
    301         for (int i = 0; i < n; ++i) {
    302             int x; scanf("%d",&x); a[x] = 1;
    303         }
    304         mc.init();
    305         solve();
    306         printf("Case #%d: %d
    ", tt, ans);
    307     }
    308     return 0;
    309 }
    hdu5836
  • 相关阅读:
    唯品会面经
    动态代理两种实现方式
    腾讯运营开发面经
    MySQL一些中重要命令
    金山wps面经
    三七互娱面经
    排序算法之快速排序(Quicksort)解析
    全排列算法分析(原创方法/一般方法/字典序法)
    WinForm如何去掉右边和下边的白边
    Java异常处理机制的秘密
  • 原文地址:https://www.cnblogs.com/wmzisfoolish/p/5775427.html
Copyright © 2011-2022 走看看