USACO Magic Squares, 难题,康托展开,位运算,BFS,哈希判重等……

来源:互联网 发布:linux rpm的使用 编辑:程序博客网 时间:2024/05/01 17:28

康托展开:http://www.nocow.cn/index.php/%E5%BA%B7%E6%89%98%E5%B1%95%E5%BC%80
这题太难了,花了几天看懂了别人的代码,牛掰到不行的位运算让我晕死

参考的别人的代码:

/*  ID: wangxin12  PROG: msquare LANG: C++  */#include <iostream>#include <fstream>#define MAX 40320using namespace std;ifstream fi("msquare.in");ofstream fo("msquare.out");class state {public:state * transFrom;short transType;unsigned long curr;state() {transFrom = NULL;transType = 0;curr = 0;}};state origin;state S[MAX];class MyQueue {private:int first, last;long list[MAX];public:long size;MyQueue() { reset(); }void reset() {first = 0; size = 0; last = -1;}void insert(state * comeFrom, unsigned long cur, short type) {last++;size++;list[last] = last;S[last].transFrom = comeFrom;S[last].transType = type;S[last].curr = cur;}long del() {size--;return list[first++];}};bool hash[MAX];char path[4] = { 0, 'A', 'B', 'C' };long fact[8] = { 1, 1, 2, 6, 24, 120, 720, 5040 };unsigned long Finish;unsigned long cantor(unsigned long s) {  //计算一个8位的不重复八进制数的康托展开long x = 0, i, p, k, j;bool hashed[8] = { false };for(i = 8; i >= 2; i--) {k = s>> 3 * (i - 1);s -= k << 3 * (i - 1);hashed[k] = true;p = k;for(j = 0; j <= k - 1; j++)if(hashed[j]) p--;x += fact[i - 1] * p;}return x;}void init() {  //读取input,并且初始化origin以及hash[0]int temp = 0, ans = 0, i;for(i = 0; i < 8; i++) {fi>>temp;temp--;ans *= 8;ans += temp;}origin.curr = ans;origin.transFrom = NULL;origin.transType = 0;Finish = cantor(origin.curr);hash[0] = true;}unsigned long transfer(unsigned long S, int type) { //感叹该位运算惊为天人,没看懂unsigned long i, P = 0;switch(type) {case 1:for (i=1;i<=8;i++)P+=(S & (7<< ( (i-1)*3) ) )>> (i-1)*3 << ((8-i)*3);return P;break;case 2:P+=(S & 07000)>>3*3;P+=(S & 00777)<<3;P+=(S & 070000)<<3*3;P+=(S & 077700000)>>3;return P;break;case 3:P= (S & 070077007);P+=(S & 070)<<5*3;P+=(S & 0700)>>1*3;P+=(S & 0700000)>>3*3;P+=(S & 07000000)>>1*3;return P;break;}}void print(unsigned long ed, long f) { //输出答案string res;state * P = &S[ed];char OUT[MAX];long count = 0, i;while(P->transType) {count++;OUT[count] = path[P->transType];P = P->transFrom;}fo<<count + 1<<endl;for(i = count; i >= 1; i--)fo<< OUT[i];fo<<path[f]<<endl;fi.close();fo.close();exit(0);}void BFS() {long i = 0, j;MyQueue Q;Q.insert(&origin, 001234567, 0);while(Q.size) {unsigned long p, ct;i = Q.del();for(j = 1; j <= 3; j++) {p = transfer(S[i].curr, j);ct = cantor(p);if(!hash[ct]) {hash[ct] = true;if(ct == Finish) print(i, j);Q.insert(&S[i], p, j);}}}}int main() {init();BFS();fo<<'0'<<endl<<endl; //没有结果的情况fi.close();fo.close();return 0;}



描述

在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:

1  2  3  4  8  7  6  5  

我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示。这是基本状态。

这里提供三种基本操作,分别用大写字母“A”,“B”,“C”来表示(可以通过这些操作改变魔板的状态):

“A”:交换上下两行;  “B”:将最右边的一列插入最左边; “C”:魔板中央四格作顺时针旋转。 

下面是对基本状态进行操作的示范:

A:  8  7  6  5      1  2  3  4  B:  4  1  2  3      5  8  7  6  C:  1  7  2  4      8  6  3  5  

对于每种可能的状态,这三种基本操作都可以使用。

你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。

[编辑]格式

PROGRAM NAME: msquare

INPUT FORMAT:

(file msquare.in)

只有一行,包括8个整数,用空格分开(这些整数在范围 1——8 之间)不换行,表示目标状态。

OUTPUT FORMAT:

(file msquare.out)

Line 1: 包括一个整数,表示最短操作序列的长度。

Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。

[编辑]SAMPLE INPUT

2 6 8 4 5 7 3 1 

[编辑]SAMPLE OUTPUT

7 BCABCCB

原创粉丝点击