八数码问题

本文深入探讨了八数码问题,包括其基本概念、解题策略和常见算法实现,旨在帮助读者理解这一经典问题。
摘要由CSDN通过智能技术生成
/*
八数码问题求解
输入输出都是随机生成
open表为优先队列pQueue
close表为键值对容器hashx
八数码状态有解的情况为起始和目标状态的排成一行后的逆序数的奇偶性相同
启发函数采用计算当前状态和目标状态的哈夫曼距离
*/
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<string.h>
using namespace std;
#define abs(x) ((x)>0?(x):-(x))
int direction[4][2]={-1,0,0,1,1,0,0,-1};
int cost[9][2];//代价
bool inverse=false;
struct Eight
{
	int num[3][3];//状态
	int hash;//哈希值
	int deep;//深度
	int hValue;//估价值
	int pre;//父亲节点
	bool inQueue;
	bool operator < (const struct Eight &a) const//比较函数
	{
		return a.hValue<hValue;
	}
	void value()//计算估价值
	{
		hValue=deep;
		for(int i=0;i<9;++i)
		{
			hValue+=abs(i/3-cost[num[i/3][i%3]][0]);
			hValue+=abs(i%3-cost[num[i/3][i%3]][1]);
		}
	}
	void getHash()//计算哈希值
	{
		hash=num[0][0];
		for(int i=1;i<9;++i)
		{
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值