HDU1043 Eight 八数码问题

本文介绍了一个经典的八数码问题解决方案,采用A*搜索算法,并详细解释了如何判断问题是否可解、如何表示状态及如何进行搜索的过程。

题目描述:给你关于一个八数码的描述,以一维形式表示出来,求解出一串可行的操作来。


分析:
首先这道题是一个special judge.
这道题卡的我出翔了…总的来说,主要部分有:是否可解的判断 、怎样搜索、怎样表示八数码的一种状态。
一、 是否可解
首先把x当做零
上网查阅后知道,以除零以外组成的一位数组的逆序对数的奇偶为标准,可以划分成两个等价类,向左或向右移动不改变逆序对数,向上或向下增加或减少两个逆序对数,所以同一等价类可以相互转换,是否可解在一开始就能判断出来,逆序对数为偶则为可解,否则不可解。
二、 搜索方法
这题好像暴力BFS能勉强过?反正我是直接上网上学的A*搜索..每种状况都有一个F值,F值越小,离答案越近。
一个情况的F值有两部分组成,G和H:
1. G:已经走过的路径长度,在八数码问题中就是从初始状态到此状态移动的步数
2. H:距目标的预算长度,在八数码问题中就是九个数离各自应该在位置的距离和。
比如:
1 2 3
4 5 6
7 8 0
这个情况的H值为0,因为每个数字都在它们自己应该在的位置上。
1 2 3
4 5 6
8 7 0
这个情况H=2,1-6全都在对应的位置上,对H的贡献为0。7应该在它左边的那个位置,距那个位置距离为1;8应该在它右面的位置上,距离为1,所以这个情况的H=2。
1 2 3
4 7 6
8 5 0
1-3、4、6都在对应的位置上,7贡献为2,5贡献为1,8贡献为1,所以H=2+1+1=4.
这样就按照每种情况放进优先队列中,按F从小到大排序。
三、表示
四个字:康拓展开。
具体是什么百度就能看懂,蛮简单的。


题目:
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

AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

bool vst[1000000];
int fac[9];

int factor(int n)
{
    int ans = 1;
    for(int i=1; i<=n; i++)
        ans *= i;
    return ans;
}

void make_factor(int fac[9])
{
    for(int i=0; i<=8; i++)
        fac[i] = factor(i);
}

class node
{
public:
    int code;
    int data[3][3];
    int x, y;
    int g, h;
    string op;
    void geth()
    {
        int ans = 0;
        for(int i=0; i<=2; i++)
        {
            for(int j=0; j<=2; j++)
            {
                ans += abs(i - (data[i][j] - 1) / 3) + abs(j - (data[i][j] - 1) % 3);
            }
        }
        h = ans;
    }
    void encode()
    {
        int ans = 0, cnt = 0;
        for(int i=0; i<=8; i++)
        {
            cnt = 0;
            for(int j=i+1; j<=8; j++)
                if(data[j/3][j%3] < data[i/3][i%3])
                    cnt ++;
            ans += cnt * fac[8 - i];
        }
        code = ans;
    }
    bool operator < (const node a)const//q1:关于优先队列的排序
    {
        return h != a.h ? h > a.h : g < a.g;
    }
}tp, tmp;

bool judge()
{
    int cnt = 0;
    for(int i=0; i<=8; i++)
    {
        if(tmp.data[i/3][i%3] == 0)
            continue;
        for(int j=i+1; j<=8; j++)
        {
            if(tmp.data[j/3][j%3] == 0)
                continue;
            if(tmp.data[i/3][i%3] > tmp.data[j/3][j%3])
                cnt ++;
        }
    }
    return ! (cnt & 1);
}

void BFS()
{
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {-1, 1, 0, 0};
    char oprt[4] = {'l', 'r', 'd', 'u'};
    priority_queue <node> que;
    que.push(tmp);
    vst[tmp.code] = true;
    while(! que.empty())
    {
        tp = que.top();
        que.pop();
        if(tp.code == 46233)
        {
            cout << tp.op << endl;
            return ;
        }
        for(int i=0; i<=3; i++)
        {
            int x = tp.x + dx[i];
            int y = tp.y + dy[i];
            if(x >= 0 && x <= 2 && y >= 0 && y <= 2)
            {
                tmp = tp;
                swap(tmp.data[tmp.x][tmp.y], tmp.data[x][y]);
                tmp.encode();
                if(! vst[tmp.code])
                {
                    tmp.x = x;
                    tmp.y = y;
                    tmp.g ++;
                    tmp.geth();
                    tmp.op += oprt[i];
                    vst[tmp.code] = true;
                    que.push(tmp);
                }
            }
        }
    }
}

int main()
{
    char ch;
    string s;
    make_factor(fac);
    while(getline(cin, s))
    {
        memset(vst, false, sizeof(vst));
        stringstream ss(s);
        for(int i=0; i<=2; i++)
        {
            for(int j=0; j<=2; j++)
            {
                ss >> ch;
                if(ch == 'x')
                {
                    tmp.data[i][j] = 0;
                    tmp.x = i;
                    tmp.y = j;
                }
                else
                    tmp.data[i][j] = ch - 48;
            }
        }
        if(! judge())
        {
            cout << "unsolvable" << endl;
            continue;
        }
        tmp.encode();
        tmp.g = 0;
        tmp.geth();
        tmp.op.clear();
        BFS();
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值