其他分享
首页 > 其他分享> > Eight HDU - 1043

Eight HDU - 1043

作者:互联网

Eight HDU - 1043

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.

Input

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

Output

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.

Sample Input

2  3  4  1  5  x  7  6  8

Sample Output

ullddrurdllurdruldr

题目大意:给你一个3*3的矩阵,有数字1~8和一个x,问最少进行多少次移动能变成如这样
在这里插入图片描述
然后把移动的过程数出出来。
思路:因为有很多个样例,如果每次从所给样例搜,会浪费很多时间,为了把时间减少,我们可以倒着搜,从最后的情况看能搜到什么情况,并把它标记起来,最后在输出路径,大大减少了时间。然后还得用到map容器①这是因为用book数组首先不能开到九位数那么大,他可以代替book起标记作用,②其次,他可以通过键值来找到不同类型的已经指定类型的映照数据的值,很方便。然后就是基本的广搜,通过已知倒推,并记录路径。还有就是一开始的处理,把x当成9或0,我帮他当成了9来处理。

map映照容器:他是有一个键值和一个映照数据组成的,键值和映照数据之间具有一一映照的关系。键值不允许重复,比较函数只对元素的键值进行比较,元素各项数据可通过键值检索出来。创建map对象,键值和映照数据的类型由自己定义。在没有制定比较函数时,元素的插入位置是按键值有大到小插入到黑白书中。

代码如下:

#include<stdio.h>//没加<string>
#include<queue>
#include<map>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
map<ll,string>m;
map<ll,int>n;
struct node
{
    string c;
    int s[3][3];
    int x,y;
};
void bfs()
{
    queue<node>Q;
    node p,q;
    int k=1;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        p.s[i][j]=k++;
    p.x=2;
    p.y=2;
    p.c="";//加上
    n[123456789]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            int tx=p.x+dir[i][0];
            int ty=p.y+dir[i][1];
            if(tx<0||tx>2||ty<0||ty>2)
                continue;
            q=p;//改了一下
            swap(q.s[tx][ty],q.s[p.x][p.y]);
            ll sum=0;
            for(int j=0;j<3;j++)
                for(int k=0;k<3;k++)
                sum=sum*10+q.s[j][k];
            if(n[sum])
                continue;
            n[sum]=1;
            if(i==0)//注意这里要倒着写
                q.c=p.c+'l';
            if(i==1)
                q.c=p.c+'u';
            if(i==2)
                q.c=p.c+'r';
            if(i==3)
                q.c=p.c+'d';
            q.x=tx;
            q.y=ty;
            Q.push(q);
            m[sum]=q.c;
        }
    }
}
int main()
{
    bfs();
    char a[50];
    while(gets(a))
    {
        ll sum=0;
        for(int i=0;i<strlen(a);i++)
        {
            if(a[i]==' ')
                continue;
            if(a[i]=='x')
                a[i]='9';
            sum=sum*10+a[i]-48;
        }
        if(!n[sum])
            printf("unsolvable\n");
        else
        {
            string ss=m[sum];
            for(int i=ss.length()-1;i>=0;i--)
                printf("%c",ss[i]);
            printf("\n");
        }
    }
    return 0;
}

标签:1043,HDU,15,int,puzzle,tiles,键值,Eight,include
来源: https://blog.csdn.net/zab1998/article/details/96735179