Eight (八数码 + 打表)

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的矩阵,求最少要对 “x” 经过怎样的移动能使矩阵变成 1 2 3 4 5 6 7 8 x。

思路:由于是多组数据,怕一般搜索会超时,因此我们可以倒着搜(从目标状态往其他状态搜,记录每种状态的反向路径),因此我们可以打一个表,当然这个表就要用map<int,string>来存了,由于只有9个数我们可以把他按顺序合成一个数并且这个数不超过int的范围,因此我们可以用一个int型整数代表每一种状态,同时我们可以用string来记录路径。切记由于是倒着搜的,因此要记录的是反向路径(u->d...),输出的时候要从后往前输出。目标状态(123456780)

代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
map<int,string>v;    //记录这种状态下的路径
map<int,int>u;      //状态标记,判断这个状态是否出现过
struct node
{
    string t;
    int s[3][3];  //矩阵
    int x,y;
};
void dfs()
{
    queue<node>Q;
    node p,q;
    int i,j,k=1;
    for(i=0;i<3;i++)   //目标状态
        for(j=0;j<3;j++)
        q.s[i][j]=k++;     
    q.s[2][2]=0;     //最后一个是x,此时用0来表示
    q.x=2;           //x的位置
    q.y=2;
    u[123456780]=1;   //目标状态
    Q.push(q);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(i=0;i<4;i++)
        {
            int x=p.x+dir[i][0];
            int y=p.y+dir[i][1];
            if(x<0||y<0||x>=3||y>=3)
                continue;
            for(j=0;j<3;j++)
                for(k=0;k<3;k++)
                q.s[j][k]=p.s[j][k];
            swap(q.s[p.x][p.y],q.s[x][y]);  //交换一下位置
            int sum=1;
            for(j=0;j<3;j++)
                for(k=0;k<3;k++)
                    sum=sum*10+q.s[j][k];   //求此时的状态(int)
            if(u[sum])     //出现过就不用入队列了
                continue;
            u[sum]=1;
            q.x=x;
            q.y=y;
            if(i==0)
                q.t=p.t+'l';    //反向存路径
            else if(i==1)
                q.t=p.t+'u';
            else if(i==2)
                q.t=p.t+'r';
            else
                q.t=p.t+'d';
            Q.push(q);
            v[sum]=q.t;   //用map记录这个状态的路径
        }
    }
}
int main()
{
    dfs();
    char str[50];
    while(gets(str))
    {
        int l=strlen(str);
        int sum=1;
        for(int i=0;i<l;i++)
            {
                if(str[i]>='1'&&str[i]<='9')
                    sum=sum*10+str[i]-'0';
                else if(str[i]=='x')
                    sum=sum*10;
            }
        if(!u[sum])  //没有这种状态
        {
            printf("unsolvable\n");
            continue;
        }
        string s=v[sum];
        l=s.size();
        for(int i=l-1;i>=0;i--)  //逆序输出路径
            printf("%c",s[i]);
        printf("\n");
    }
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值