1、#include #include #include typedef struct Node/节点结构体int data9;double f,g;struct Node * parent;Node,*Lnode;typedef struct Stack/OPEN CLOSED 表结构体Node * npoint;struct Stack * next;Stack,* Lstack;Node * Minf(Lstack * Open)/选取OPEN表上f值最小的节点,返回该节点地址Lstack temp = (*Open)-next,min = (*Open)-next,minp = (*Ope
2、n);Node * minx;while(temp-next != NULL)if(temp-next -npoint-f) npoint-f)min = temp-next;minp = temp;temp = temp-next;minx = min-npoint;temp = minp-next;minp-next = minp-next-next;free(temp);return minx;int Canslove(Node * suc, Node * goal)/判断是否可解int a = 0,b = 0,i,j;for(i = 1; idatai suc-dataj) if(go
3、al-datai goal-dataj) if(a%2 = b%2)return 1;else return 0;int Equal(Node * suc,Node * goal)/判断节点是否相等,相等,不相等for(int i = 0; i datai != goal-datai)return 0;return 1;Node * Belong(Node * suc,Lstack * list)/判断节点是否属于OPEN表或CLOSED表,是则返回节点地址,否则返回空地址Lstack temp = (*list) - next ;if(temp = NULL)return NULL;whil
4、e(temp != NULL)if(Equal(suc,temp-npoint)return temp - npoint;temp = temp-next;return NULL;void Putinto(Node * suc,Lstack * list)/把节点放入OPEN 或CLOSED 表中Stack * temp;temp =(Stack *) malloc(sizeof(Stack);temp-npoint = suc;temp-next = (*list)-next;(*list)-next = temp;/计算f值部分-开始/double Fvalue(Node suc, Nod
5、e goal, float speed)/计算f值double Distance(Node,Node,int);double h = 0;for(int i = 1; i g) g)temp-parent = (*suc)-parent;temp-g = (*suc)-g;temp-f = (*suc)-f;flag = 1;elsePutinto(* suc, Open);(*suc)-f = Fvalue(*suc, goal, speed);return flag; int Canspread(Node suc, int n)/判断空格可否向该方向移动,表示空格向上向下向左向右移int
6、i,flag = 0;for(i = 0;i datai = child-parent-datai;for(i = 0;i datai = 0)break;if(n=0)loc = i%3+(i/3 - 1)*3;else if(n=1)loc = i%3+(i/3 + 1)*3;else if(n=2)loc = i%3-1+(i/3)*3;elseloc = i%3+1+(i/3)*3;temp = child-dataloc;child-datai = temp;child-dataloc = 0;void Spread(Lnode * suc, Lstack * Open, Lstac
7、k * Closed, Node goal, float speed)/扩展后继节点总函数int i;Node * child;for(i = 0; i g = (*suc)-g +1; /算子节点的g值child-parent = (*suc); /子节点父指针指向父节点Spreadchild(child, i); /向该方向移动空格生成子节点if(BelongProgram(/扩展后继节点部分的函数-结束/Node * Process(Lnode * org, Lnode * goal, Lstack * Open, Lstack * Closed, float speed)/总执行函数w
8、hile(1)if(*Open)-next = NULL)return NULL; /判断OPEN表是否为空,为空则失败退出Node * minf = Minf(Open); /从OPEN表中取出f值最小的节点Putinto(minf, Closed); /将节点放入CLOSED表中if(Equal(minf, *goal)return minf; /如果当前节点是目标节点,则成功退出Spread( /当前节点不是目标节点时扩展当前节点的后继节点int Shownum(Node * result)/递归显示从初始状态到达目标状态的移动方法if(result = NULL)return 0;el
9、seint n = Shownum(result-parent);for(int i = 0; i datai*3+j != 0)printf(“ %d “,result-datai*3+j);else printf(“ “);printf(“n“);return n+1;void Checkinput(Node *suc)/检查输入int i = 0,j = 0,flag = 0;char c;while(i = 0)flag = 0;else if(c = 0 flag = 1;for(j =0; j dataj = suc-datai)flag = -2;i+;else if(flag
10、= 0)flag = -1;elseif(flag = 0)flag = -1;if(flag next = NULL;Lstack Closed = (Stack *) malloc(sizeof(Stack);Closed-next = NULL;Node * org = (Node *) malloc(sizeof(Node);org-parent = NULL; /初始状态节点org-f =1;org-g =1;Node * goal = (Node *) malloc(sizeof(Node); /目标状态节点Node * result;float speed = 1;/speed搜
11、索速度char c;printf(“=n“);printf(“说明:状态矩阵由0-8 九个数字表示,n请依次按照九宫格上的行列顺序输入,每个数字间用空格隔开。n“);printf(“=n“);printf(“请输入初始状态(0-8 9个数字以空格隔开回车表示输入结束):n“);Checkinput(org);printf(“请输入目标状态(0-8 9个数字以空格隔开回车表示输入结束):n“);Checkinput(goal);if(Canslove(org, goal)/A*算法开始,先将初始状态放入OPEN表 printf(“请输入搜索速度(速度越高越无法保证结果是最优解):“);scanf(“%f“,while(c = getchar() != 10);printf(“搜索中,请耐心等待(如果您觉得时间太久请重新执行程序并输入更快的速度,默认值为)n“);Putinto(org,result = Process( /进行剩余的操作printf(“总步数:%d,搜索速度:%fn“,Shownum(result),speed);printf(“n“);printf(“Press Enter key to exit!“);while(c = getchar() != 10);else printf(“程序认定该起始状态无法道达目标状态!n“);