用c++实现A*算法的九宫重排

时间: 2023-07-20 09:26:21 浏览: 68
下面是用C++实现A*算法的九宫重排的代码: ```c++ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<queue> #include<algorithm> #define MAXN 3 #define MAXS 500000 #define MAXH 999983 using namespace std; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; struct node { int map[MAXN][MAXN]; int hx; int x,y; int g,f; int pre; }start,goal,temp; int cnt,head[MAXH],next[MAXS],state[MAXS]; node e[MAXS]; int ans[MAXS]; void init() { cnt=0; memset(head,-1,sizeof(head)); } int get_hash(node u) { int k=0; for(int i=0;i<MAXN;i++) for(int j=0;j<MAXN;j++) k=k*10+u.map[i][j]; return k%MAXH; } int find(node u) { int h=get_hash(u); for(int i=head[h];i!=-1;i=next[i]) if(memcmp(e[i].map,u.map,sizeof(u.map))==0) return i; return -1; } void insert(node u) { int h=get_hash(u); e[cnt]=u; state[cnt]=u.hx; next[cnt]=head[h]; head[h]=cnt++; } int h(node u) { int ans=0; for(int i=0;i<MAXN;i++) for(int j=0;j<MAXN;j++) { int x=(u.map[i][j]-1)/MAXN; int y=(u.map[i][j]-1)%MAXN; ans+=abs(x-i)+abs(y-j); } return ans; } void A_star() { priority_queue<pair<int,int> ,vector<pair<int,int> > ,greater<pair<int,int> > > q; memset(ans,-1,sizeof(ans)); start.g=0; start.f=h(start); start.hx=get_hash(start); insert(start); q.push(make_pair(start.f,0)); while(!q.empty()) { int f=q.top().first; int u=q.top().second; q.pop(); if(ans[state[u]]!=-1) continue; ans[state[u]]=e[u].g; for(int i=0;i<4;i++) { int nx=e[u].x+dx[i]; int ny=e[u].y+dy[i]; if(nx<0||nx>=MAXN||ny<0||ny>=MAXN) continue; temp=e[u]; temp.g=e[u].g+1; temp.f=temp.g+h(temp); swap(temp.map[e[u].x][e[u].y],temp.map[nx][ny]); temp.x=nx; temp.y=ny; temp.pre=u; int v=find(temp); if(v==-1) { temp.hx=get_hash(temp); insert(temp); q.push(make_pair(temp.f,cnt-1)); } else if(ans[state[v]]==-1||ans[state[v]]>temp.g) { e[v]=temp; ans[state[v]]=temp.g; q.push(make_pair(temp.f,v)); } } } } int main() { init(); int x; for(int i=0;i<MAXN;i++) for(int j=0;j<MAXN;j++) { scanf("%d",&x); start.map[i][j]=x; if(x==0) { start.x=i; start.y=j; } } for(int i=0;i<MAXN;i++) for(int j=0;j<MAXN;j++) { scanf("%d",&x); goal.map[i][j]=x; if(x==0) { goal.x=i; goal.y=j; } } A_star(); int t=find(goal); printf("%d\n",ans[state[t]]); return 0; } ``` 这里的A*算法是用了一个优先队列来存储结点,每次取出f值最小的结点进行扩展。在扩展一个结点时,我们首先计算出它的估价函数值h,然后计算出g值和f值,根据f值把结点插入到优先队列中,同时在插入时记录下当前状态的哈希值,以便后面进行查找。我们还需要记录下每个状态到达目标状态的步数,这里使用了一个ans数组来记录。在查找的过程中,如果发现当前状态已经被访问过,并且到达目标状态的步数比之前更小,我们就更新这个状态。最后,当我们找到目标状态时,输出到达目标状态的步数即可。

相关推荐

最新推荐

recommend-type

用C++实现DBSCAN聚类算法

本篇文章是对使用C++实现DBSCAN聚类算法的方法进行了详细的分析介绍,需要的朋友参考下
recommend-type

C++实现分水岭算法(Watershed Algorithm)

主要为大家详细介绍了C++实现分水岭算法Watershed Algorithm,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

使用C++实现全排列算法的方法详解

本篇文章是对使用C++实现全排列算法的方法进行了详细的分析介绍,需要的朋友参考下
recommend-type

人工智能 A*算法 八数码问题 C++ 报告+代码+详细注释

使用C++语言完整的实现了A星算法解决八数码问题 内容:完整代码和详细注释; 主要函数的功能说明; 评价函数的设计; 运行测试结果
recommend-type

基于C++实现五子棋AI算法思想

主要为大家详细介绍了基于C++实现五子棋AI算法思想,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。