【CodeVS1225】八数码难题

Description

Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.
问题描述

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。

Input

输入初试状态,一行九个数字,空格用0表示

Output

只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据)

Sample Input

283104765

Sample Output

4

HINT

详见试题

题解

bfs哈希判重

#include<iostream>
#include<cstdio>
#define N 500000
using namespace std;
struct node
{
    int mp[4][4];
}a[N];
int g[4][4]={{0,0,0,0},{0,1,2,3},{0,8,0,4},{0,7,6,5}};
int xx[4] = {0,0,1,-1};
int yy[4] = {1,-1,0,0};
int hash[3733800];
int step[N];
int h,t=1,flag;
int check()
{
    for (int i=1;i<=3;i++)
        for (int j=1;j<=3;j++)
        if (a[t].mp[i][j] != g[i][j])
            return 0;
    return 1;
}
int Hash()
{
    int s = 0,k = 1;
    for (int i=1;i<=3;i++)
        for (int j=1;j<=3;j++)
            s += a[t].mp[i][j]*k,k*=7;
    s %= 3733799;
    if (!hash[s])    {hash[s] = 1;return 1;}
    return 0;
}
int pd(int x,int y)
{
    if (x && x<=3 && y && y<=3) return 1;
    return 0;
}
void move(int x,int y)
{
    for (int i=0;i<4;i++)
    {
        int p = x+xx[i], q = y+yy[i];
        if (pd(p,q))
        {
            for (int j=1;j<=3;j++)
                for (int k=1;k<=3;k++)
                a[t].mp[j][k] = a[h].mp[j][k];
            swap(a[t].mp[x][y],a[t].mp[p][q]);
            step[t] = step[h] +1;
            if (check())    {cout<<step[t]; flag = 1;return;}
            if (Hash()) t++;
        }
    }
}
void search()
{
    while (h<t)
    {
        for (int i=1;i<=3;i++)
            for (int j=1;j<=3;j++)
            {
                if (a[h].mp[i][j] == 0)
                    move(i,j);
                if (flag) return;
            }
        h++;
    }
}
int main()
{
    string str;
    cin>>str;
    for (int i=1;i<=3;i++)
        for (int j=1;j<=3;j++)
        a[0].mp[i][j] = str[(i-1)*3+j-1]-'0';
    search();
}
原文地址:https://www.cnblogs.com/liumengyue/p/5574940.html