USACO / Magic Squares(经典BFS+Cantor展开hash)

描述

在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板。这是一张有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

 

这道题是BFS+Hash

在做哈希时,以为8的全排列为40320,所以找到序列是全排列中第几个元素就可以。

这题用到一种hash 函数,对很多字符串或者大数的题都非常有用——康托展开 具体可以看博客里另一篇随笔“康托展开”。

用一个布尔数组vis来判重,用整形数组b[50000][2]来记录具体路径,我这里b[i][0]记录的是hash值为i的魔板状态的前一步的hash值,b[i][1]记录从上一状态得到hash值为i的魔板状态的变化方法。

这样在输出时只需从后往前找,一直找到b[1][0]=0结束,然后逆序输出即可。

此题不得不说是一道很经典的BFS题目,以后也应该多翻出来看一看~~~

 

CODE:

/*
ID:138_3531
PROG:msquare
LANG:C++
*/

#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <climits>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

int trans(int a[],int n)
{
    int num=0;
    int k=1;
    for (int i=n-1;i>=0;i--)
    {
        num+=a[i]*k;
        k*=10;
    }
    return num;
}

int cantor[]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800};           //=n!

//康托展开,求数列是第几大数
//参数int puzz[]为待展开之数的各位数字,如需展开2134,则puzz[4]={2,1,3,4}.

int Can(string puzz)           //psize puzz集合的大小
{
    int psize=puzz.size();
    int ct = 0;
    for(int i = 0; i < psize-1; i++){
        int tmp = 0;
        for(int j = i + 1; j < psize; j++){
            if( puzz[j] < puzz[i]) tmp++;
        }
        ct += tmp * cantor[psize-i-1];
    }
    return ct;
}

ifstream fin("msquare.in");
ofstream fout("msquare.out");

queue <string> q;
string purpose;
bool vis[50000];
int b[50000][2];

void bfs()
{
    string n;
    q.push("12345678");
    vis[0]=true;
    while(!q.empty())
    {
        n=q.front();
        q.pop();
        int k=Can(n);
        if (n==purpose)     break;
        //A
        string n1(n);
        reverse(n1.begin(),n1.end());
        int j=Can(n1);
        if (!vis[j])
        {
            q.push(n1);
            vis[j]=true;
            b[j][0]=k;
            b[j][1]=1;
        }
        //B
        n1=n;
        n1[0]=n[3];n1[3]=n[2];n1[2]=n[1];n1[1]=n[0];
        n1[7]=n[4];n1[4]=n[5];n1[5]=n[6];n1[6]=n[7];
        j=Can(n1);
        if (!vis[j])
        {
            q.push(n1);
            vis[j]=true;
            b[j][0]=k;
            b[j][1]=2;
        }
        //C
        n1=n;
        n1[1]=n[6];n1[2]=n[1];n1[5]=n[2];n1[6]=n[5];
        j=Can(n1);
        if (!vis[j])
        {
            q.push(n1);
            vis[j]=true;
            b[j][0]=k;
            b[j][1]=3;
        }
    }
}

int main()
{
    string a="12345678";
    for (int i=0;i<8;i++)
        fin>>a[i];
    purpose=a;
    bfs();
    stack <char> S;
    int k=Can(purpose);
    while(k!=0)
    {
        S.push(char(b[k][1]+64));
        k=b[k][0];
    }
    fout<<S.size()<<endl;
    while(!S.empty())
    {
        fout<<S.top();
        S.pop();
    }
    fout<<endl;
    return 0;
}

 

 

举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
原文地址:https://www.cnblogs.com/AbandonZHANG/p/2601876.html