Eight (hdu-1043)

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

简化题意可以为给你一个序列 你要把它变为12345678x,并且每次只能移动x
输出最短的步骤字符串 上下左右分别为 udlr

我们可以用map容易把x变为9 就从一个9位数变为123456789,可以用队列搜索,
把存下的字符用map容器存起来,
但是如果你尝试了就会发现用该状态去搜123456789会超限,因为是多组输入嘛。
所以我们可以反过来用123456789去搜所有状态,输入后直接变为整形用map判断
如果为真倒着把串输出就行,如果为假输出-1

完整代码如下:


#include
#include
#include
#include
#include
#include
using namespace std;
mapmp;
mapqq;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上 下 左 右
struct node
{
    int s[3][3];
    int x,y;
    int step;
};
node now;
int check(node w)
{
    int sum=0;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        sum=sum*10+w.s[i][j];
    return sum;
}
void bfs()
{
    node next;
    queueq;
    int k=1;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        now.s[i][j]=k++;
    now.step=0;
    now.x=2;now.y=2;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int dx=now.x+dir[i][0];
            int dy=now.y+dir[i][1];
            if(dx<0||dy<0||dx>=3||dy>=3)
               continue;
                for(int j=0;j<3;j++)
            for(int k=0;k<3;k++)
                next.s[j][k]=now.s[j][k];
            next.step=now.step+1;
            next.x=dx;next.y=dy;
            if(i==0){swap(next.s[dx][dy],next.s[now.x][now.y]);
                if(mp[check(next)])continue;
                qq[check(next)]=qq[check(now)]+'d';
                mp[check(next)]=1;
                q.push(next);
                 }//上
            else if(i==1){swap(next.s[dx][dy],next.s[now.x][now.y]);
                if(mp[check(next)])continue;
                qq[check(next)]=qq[check(now)]+'u';
                mp[check(next)]=1; q.push(next);
                 }//下
            else if(i==2){ swap(next.s[dx][dy],next.s[now.x][now.y]);
                if(mp[check(next)])continue;
               qq[check(next)]=qq[check(now)]+'r';
                mp[check(next)]=1; q.push(next);
                 }//左
            else if(i==3){swap(next.s[dx][dy],next.s[now.x][now.y]);
                if(mp[check(next)])continue;
                qq[check(next)]=qq[check(now)]+'l';
                mp[check(next)]=1;q.push(next);
                 }//右
        }
    }
}
int main()
{
    bfs();
    char s[50];
    while(gets(s))
    {
        int sum=0;
        for(int i=0;i=0;i--)
                printf("%c",w[i]);
            printf("\n");
        }
    }
}

你可能感兴趣的:(Eight (hdu-1043))