人工智能A星算法解决八数码难题程序代码 下载本文

内容发布更新时间 : 2024/9/18 5:06:59星期一 下面是文章的全部内容请认真阅读。

#include \#include \#include \#include \

void Copy_node(struct node *p1,struct node *p2); void Calculate_f(int deepth,struct node *p); void Add_to_open(struct node *p); void Add_to_closed(struct node *p);

void Remove_p(struct node *name,struct node *p); int Test_A_B(struct node *p1,struct node *p2);

struct node * Search_A(struct node *name,struct node *temp); void Print_result(struct node *p);

struct node // 定义8数码的节点状态 {

int s[3][3]; //当前8数码的状态 int i_0; //当前空格所在行号 int j_0; //当前空格所在列号 int f; //当前代价值 int d; //当前节点深度

-可编辑修改-

int h; //启发信息,采用数码\不在位\距离和 struct node *father; //指向解路径上该节点的父节点

struct node *next; //指向所在open或closed表中的下一个元素 } ;

struct node s_0={{2,8,3,1,6,4,7,0,5},2,1,0,0,0,NULL,NULL}; //定义初始状态 struct node s_g={{1,2,3,8,0,4,7,6,5},1,1,0,0,0,NULL,NULL}; //定义目标状态 struct node *open=NULL; //建立open表指针 struct node *closed=NULL; //建立closed表指针 int sum_node=0; //用于记录扩展节点总数

//*********************************************************** //********************** ********************** //********************** 主函数开始 ********************** //********************** ********************** //***********************************************************

void main() {

-可编辑修改-

int bingo=0; //定义查找成功标志,bingo=1,成功

struct node s; //定义头结点s struct node *target,*n,*ls,*temp,*same; //定义结构体指针

Copy_node(&s_0,&s); //复制初始状s_0态给头结点s Calculate_f(0,&s); //计算头结点的代价值 Add_to_open(&s); //将头结点s放入open表 while(open!=NULL) //只要open表不为空,进行以下循环 {

n=open; //n指向open表中当前要扩展的元素 ls=open->next;

Add_to_closed(n);

open=ls; //将n指向的节点放入closed表中

if(Test_A_B(n,&s_g)) //当前n指向节点为目标时,跳出程序结束;否则,继续下面的步骤

{

bingo=1;

break;

}

else

if(n->j_0>=1) //空格所在列号不小于1,可左移

-可编辑修改-