Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏

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
Difficulty:
1.康拓展开:类似于状态压缩将一个数组映射为一个数。
适用条件:当知道这个数组的个数是一定的。
2.一位数组和二维数组的互换。
http://wenku.baidu.com/link?url=YeNqa-MsEwqOZTvOR__alZS12Xog8OZDlnIXVA5WORuM0lCPu9LmkFrSsbYFBuFVj_h5F0hf6I4NVZD076lwG_xEAmWJft2HtRKWMIWAkBa
思路:将二维数组看成一个状态,再进行bfs,同时要将二维数组先变形为一位数组再根据康拓展开变形为一个数。
打表,反向搜索。
注意:这里有多解,不止一个答案,方向顺序不同,答案不同。

#include <cstdio>
#include <iostream>
#include<cstring>
using namespace std;
struct Node
{
    int a[9];
}Q[500000];//转态表示
int n,eid,cnt,flag,x0,ya,z0,tmpid,x,y;
int vis[500000];
Node s,t;
char ss[5];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
char dir[]="lrdu";//反向搜索,所以反着写。
char di[500000];
int A[9];
int net[500000];
int getid(int *a)//康拓展开
{
    int v=0;
    for(int i=0;i<=8;i++)
    {
        int cnt=0;
        for(int j=i+1;j<=8;j++)
            if(a[i]>a[j])
                cnt++;
        v+=cnt*A[8-i];
    }
    return v;
}
void bfs()
{
    memset(vis,0,sizeof(vis));
    int front=0,rear=0;
    Q[rear++]=s;
    //net[eid]=-1;
    vis[eid]=1;
    while(front<rear)
    {
        Node q=Q[front++];
        tmpid=getid(q.a);//一维数组映射为一个数
        for(int i=0;i<9;i++)
    {
        if(q.a[i]==0)
        {x0=i/3;
         ya=i%3;//一维数组转换为二维数组。
         z0=i;
        break;
    }}
       Node add=q;;
        for(int i=0;i<4;i++)
        {
             x=x0+dx[i];
             y=ya+dy[i];
            if(x<0||x>=3||y<0||y>=3)
            continue;
            int z=x*3+y;//二维数组转换为一维数组
            add.a[z0]=q.a[z];
            add.a[z]=0;
            int qid=getid(add.a);
            if(vis[qid]==0)
            {
                vis[qid]=1;
                net[qid]=tmpid;//转态转移
                di[qid]=dir[i];//记录操作
                Q[rear++]=add;
            }
        add=q;
    }
    //front++;
}
}
int main()
{ A[0]=1;
    for(int i=1;i<=8;i++)
        A[i]=A[i-1]*i;//康拓展模板
for(int i=0;i<=8;i++)
if(i==8)
s.a[i]=0;
else
s.a[i]=i+1;
 eid=getid(s.a);
bfs();
while(~scanf("%s",ss))
{
    if(ss[0]=='x')
    s.a[0]=0;
    else
    s.a[0]=ss[0]-'0';
    for(int i=1;i<=8;i++)
    {
    scanf("%s",ss);//可过滤空格
    if(ss[0]=='x')
    s.a[i]=0;
    else
    s.a[i]=ss[0]-'0';
    }
    int sid=getid(s.a);
    if(vis[sid])
    {
        while(sid!=eid)//上面打的表中推出一步一步的转态转换。
        {
            putchar(di[sid]);
            sid=net[sid];
        }
    }
    else
    printf("unsolvable");
    puts("");
    }
  return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-07-05 22:34  JoneZP  阅读(226)  评论(0编辑  收藏  举报