其他分享
首页 > 其他分享> > [kuangbin]专题二 搜索进阶 Eight HDU - 1043【反向BFS】【康托展开】【哈希】

[kuangbin]专题二 搜索进阶 Eight HDU - 1043【反向BFS】【康托展开】【哈希】

作者:互联网

【题目描述】
The 15-puzzle has been around for over 100 years;even if you don’t know it by that name,you’ve seen it.It is constructed with 15 sliding tiles,each with a number from 1 to 15 on it,and all packed into a 4 by 4 frame with one tile missing.Let’s call the missing tile ‘x’;the object of the puzzle is to arrange the tiles so that they are ordered as:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x

where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge.
As an example,the following sequence of moves solves a slightly scrambled puzzle:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
            r->            d->            r->

The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step;legal values are ‘r’,‘l’,‘u’ and ‘d’,for right,left,up,and down,respectively.
Not all puzzles can be solved;in 1870,a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle,and frustrating many people.In fact,all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile,of course).
In this problem,you will write a program for solving the less well-known 8-puzzle,composed of tiles on a three by three arrangement.
这个15字谜已经存在了100多年;即使你不知道这个名字,你也知道它。它是用15个滑动瓷砖建造的,每个瓷砖上的数字从1到15不等,所有这些都被包装成一个4×4的框架,一个瓷砖缺失了。让我们将丢失的瓷砖称为“x”;拼图的目标是排列这些瓷砖,以便按如下顺序排列:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x

唯一合法的操作是将“x”与其共享边缘的瓷砖之一交换。作为一个例子,下面的移动顺序解决了一个稍微混乱的谜题:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
            r->            d->            r->

字母表示x和哪个相邻的数字交换,合法的操作分别为“r”、“l”、“u”和“d”,分别表示右、左、上和下。
并不是所有的谜题都能解决;1870年,一个名叫山姆·洛伊德的人因散发无法解决的谜题而出名,并使许多人感到沮丧。实际上,要将一个常规的谜题变成一个无法解决的难题,所要做的就是交换两个瓷砖(当然,不包括丢失的‘x’瓷砖)。在这个问题中,你将编写一个程序来解决不太知名的8-谜题,由三次排列的瓷砖组成。

【输入】
You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x’. For example, this puzzle
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8

【输出】
You will print to standard output either the word ``unsolvable’’, if the puzzle has no solution, or a string consisting entirely of the letters ‘r’, ‘l’, ‘u’ and ‘d’ that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases.

【样例输入】
2 3 4 1 5 x 7 6 8

【样例输出】
ullddrurdllurdruldr

题目链接:https://cn.vjudge.net/problem/HDU-1043

写了两天看了无数大佬的代码终于过了QAQ,还是太菜了
临时学的康托展开,不会看这里 → https://www.cnblogs.com/sky-stars/p/11216035.html
照着这个写的 → https://www.cnblogs.com/sky-stars/p/11216007.html

代码如下:

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int mp[10];
static const int MAXN=362880;
static const int result=46234;
static const int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
static const int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
static string op="lrud"; //要反着写
bool vis[MAXN+10];
string path[MAXN+10];
struct Node{
    string path;
    int hash;
    int pos;
};
void decantor(int hashs)
{
    vector<int> v;
    for(int i=0;i<=8;i++)
        v.push_back(i);
    for(int i=0;i<=8;i++)
    {
        int r=hashs%fac[8-i];
        int t=hashs/fac[8-i];
        hashs=r;
        sort(v.begin(),v.end());
        mp[i]=v[t];
        v.erase(v.begin()+t);
    }
}
int cantor()
{
    int sum=0;
    for(int i=0;i<=8;i++)
    {
        int cnt=0;
        for(int j=i+1;j<=8;j++)
        {
            if(mp[j]<mp[i])
                cnt++;
        }
        sum+=fac[8-i]*cnt;
    }
    return sum+1;
}
void bfs()//反向BFS,先把所有可能到达情况存下来
{
    queue<Node> Q;
    memset(vis,false,sizeof(vis));
    for(int i=0;i<8;i++)
        mp[i]=i+1;
    mp[8]=0;
    vis[result]=true;
    path[result]="";
    Q.push(Node{"",result,8});
    while(!Q.empty())
    {
        Node now=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            Node next;
            if(0<=now.pos/3+dx[i] && now.pos/3+dx[i]<=2 && 0<=now.pos%3+dy[i] && now.pos%3+dy[i]<=2)
            {
                next=now;
                next.pos=(now.pos/3+dx[i])*3+now.pos%3+dy[i];
                decantor(now.hash-1);
                swap(mp[now.pos],mp[next.pos]);
                next.hash=cantor();
                if(!vis[next.hash])
                {
                    vis[next.hash]=true;
                    next.path=op[i]+next.path;
                    path[next.hash]=next.path;
                    Q.push(next);
                }
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    bfs();
    char ch;
    while(cin>>ch)//多组数据输入
    {
        if(ch=='x')
                mp[0]=0;
            else
                mp[0]=ch-'0';
        for(int i=1;i<=8;i++)
        {
            cin>>ch;
            if(ch=='x')
                mp[i]=0;
            else
                mp[i]=ch-'0';
        }
        int hashs=cantor();
        if(!vis[hashs])
            cout<<"unsolvable"<<endl;//vis没有被改动则说明无法到达此情况
        else
            cout<<path[hashs]<<endl;
    }
    return 0;
}

标签:1043,10,HDU,12,15,int,13,11,康托
来源: https://blog.csdn.net/inv00ker/article/details/98470437