资源简介
本科生计算机相关专业 人工智能课程 A*算法解决迷宫问题C++代码 详细注释,易懂

代码片段和文件信息
#include
#include
#include
using namespace std;
struct direction //存储可以扩展的方向的数据结构
{
public:
bool uprightdownleft;
bool closed; //用来记录是否是closed集中
direction(){};
direction(int aint bint cint d)
{
up=a;
right=b;
down=c;
left=d;
closed=0;
}
};
class point
{
public:
int xygvalue;
point* father;
point(){value=INT_MAX;};
point(int aint b)
{
x=a;y=b;
value=INT_MAX;
};
bool operator < (point &b)
{
return value };
void f(point a);
};
void point::f(point a) //计算f值,使用形参为了可以使终点可自由改变
{
value=g+abs(a.x-x)+abs(a.y-y);
}
void main()
{
direction rule[5][5]; //为了便于理解就浪费点空间吧……
queue result; //即closed集,也就是结果路径。
list open;
//规则集//////////////////////////////////////////////////////////////
rule[1][1]=direction(1000);
rule[1][2]=direction(1010);
rule[1][3]=direction(0110);
rule[1][4]=direction(0100);
rule[2][1]=direction(1100);
rule[2][2]=direction(1110);
rule[2][3]=direction(1011);
rule[2][4]=direction(0111);
rule[3][1]=direction(1101);
rule[3][2]=direction(1011);
rule[3][3]=direction(1110);
rule[3][4]=direction(0011);
rule[4][1]=direction(1001);
rule[4][2]=direction(1010);
rule[4][3]=direction(1011);
rule[4][4]=direction(0010);
////////////////////////////////////////////////////////////////////////
point temp;
direction d_temp;
point s(11);
point terminal(44);
s.g=0;
s.father=NULL;
s.f(terminal);
open.push_back(s);
while(!(open.empty()|(temp.x==terminal.x&&temp.y==terminal.y)))
{
open.sort();
temp=open.front();
result.push(open.front());
rule[open.front().x][open.front().y].closed=1;//表明此结点已加入closed集
d_temp=rule[temp.x][temp.y];
open.pop_front();
if(d_temp.up)
{
point t(temp.xtemp.y+1);
t.g=temp.g+1;
t.f(terminal);
if(!rule[t.x][t.y].closed) //判断结点是否在closed集中
{
open.push_front(t);
}
}
if(d_temp.right)
{
point t(temp.x+1temp.y);
t.g=temp.g+1;
t.f(terminal);
if(!rule[t.x][t.y].closed)
{
open.push_front(t);
}
}
if(d_temp.down)
{
point t(temp.xtemp.y-1);
t.g=temp.g+1;
t.f(terminal);
if(!rule[t.x][t.y].closed)
{
open.push_front(t);
}
}
if(d_temp.left)
{
point t(temp.x-1temp.y);
t.g=temp.g+1;
t.f(terminal);
if(!rule[t.x][t.y].closed)
{
open.push_front(t);
}
}
}
if(open.empty()) cout<<“没有路径!“< else
{
cout<<“最短路径:“< while(!result.empty())
{
cout<<“(“<“;
result.pop();
}
cout<<“\b\b“<<“ “< }
getchar();
getchar();
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2977 2018-11-23 15:06 maze.cpp
- 上一篇:svd分解的C语言实现
- 下一篇:c语言运动会分数统计
相关资源
- 数据结构,迷宫问题C语言版源代码
- 走迷宫(自动生成迷宫版本!)
- c++迷宫最短路径寻径算法
- OpenGL迷宫山东大学图形学实验三
- C语言自动走迷宫程序代码
- MFC可视化_迷宫算法_最短路径
- 迷宫MFC实现最短路径,有简单界面迷
- 迷宫益智游戏,c++mfc编写,亲测有效
- C++ 数据结构 迷宫求解
- MFC单文档程序下实现自动随机生成迷
- MFC 编写的迷宫游戏
- opengl做的迷宫游戏,基于C++
- Qt实现的迷宫与魔塔游戏 源码迷宫模
- 随机迷宫的形成及迷宫路径查找与输
- mfc 实现迷宫程序
- 迷宫求解MFC
- c++写的迷宫
- MFC实现迷宫搜索——Easy参考
- 3D天空迷宫.zip
- 基于VS2010的c++程序迷宫游戏
- c/c++解决迷宫问题
- 数据结构迷宫算法源码+实验报告
- 走迷宫问题栈实现.cpp
- 数据结构迷宫代码
- 迷宫问题代码算法详解
- 迷宫问题_数据结构C++课程设计_带报告
- 用栈非递归方法迷宫找出路
- 利用队列实现迷宫问题
- C++实现简单走迷宫的代码
- C语言迷宫小游戏课程设计
评论
共有 条评论