1、C语言实现8数码问题之欧阳物创编1、实验目的时间:2021.02.07命题人:欧阳物(1)熟悉人工智能系统中的问题求解过程;(2)熟悉状态空间中的盲目搜索策略;(3)掌握盲目搜索算法,重点是宽度优先搜索和深度优先搜索算法。2、实验要求用VC语言编程,采用宽度优先搜索和深度优先搜索方法,求解8数码问题3、实验内容(1)采用宽度优先算法,运行程序,要求输入初始状态假设给定如下初始状态S02 8 31 6 47 0 5和目标状态Sg2 1 6 4 0 8 7 5 3 验证程序的输出结果,写出心得体会。(2)对代码进行修改(选作),实现深度优先搜索求解该问题提示:每次选扩展节点时,从数组的最后一个生成
2、的节点开始找,找一个没有被扩展的节点。这样也需要对节点添加一个是否被扩展过的标志。4 源代码及实验结果截图#include#include#include/八数码状态对应的节点结构体struct Node int s33;/保存八数码状态,0代表空格 int f,g;/启发函数中的f和g值 struct Node * next; struct Node *previous;/保存其父节点 ;int open_N=0; /记录Open列表中节点数目/八数码初始状态int inital_s33= 2,8,3,1,6,4,7,0,5;/八数码目标状态int final_s33= 2,1,6,4,0,
3、8,7,5,3;/-/添加节点函数入口,方法:通过插入排序向指定表添加/-void Add_Node( struct Node *head, struct Node *p) struct Node *q; if(head-next)/考虑链表为空 q = head-next; if(p-f next-f)/考虑插入的节点值比链表的第一个节点值小 p-next = head-next; head-next = p; else while(q-next)/考虑插入节点x,形如a= x f f |q-f = p-f) & (q-next-f p-f | q-next-f = p-f) p-next
4、= q-next; q-next = p; break; q = q-next; if(q-next = NULL) /考虑插入的节点值比链表最后一个元素的值更大 q-next = p; else head-next = p;/-/删除节点函数入口/-void del_Node(struct Node * head, struct Node *p ) struct Node *q; q = head; while(q-next) if(q-next = p) q-next = p-next; p-next = NULL; if(q-next = NULL) return; / free(p);
5、 q = q-next; /-/判断两个数组是否相等函数入口/-int equal(int s133, int s233) int i,j,flag=0; for(i=0; i 3 ; i+) for(j=0; jnext; int flag = 0; while(q) if(equal(q-s,s) flag=1; Old_Node-next = q; return 1; else q = q-next; if(!flag) return 0;/-/计算p(n)的函数入口/其中p(n)为放错位的数码与其正确的位置之间距离之和/具体方法:放错位的数码与其正确的位置对应下标差的绝对值之和/-in
6、t wrong_sum(int s33) int i,j,fi,fj,sum=0; for(i=0 ; i3; i+) for(j=0; j3; j+) for(fi=0; fi3; fi+) for(fj=0; fj3; fj+) if(final_sfifj = sij) sum += fabs(i - fi) + fabs(j - fj);break; return sum;/-/获取后继结点函数入口/检查空格每种移动的合法性,如果合法则移动空格得到后继结点/-int get_successor(struct Node * BESTNODE, int direction, struct
7、Node *Successor)/扩展BESTNODE,产生其后继结点SUCCESSOR int i,j,i_0,j_0,temp; for(i=0; i3; i+) for(j=0; jsij = BESTNODE-sij;/获取空格所在位置 for(i=0; i3; i+) for(j=0; jsij = 0)i_0 = i; j_0 = j;break; switch(direction) case 0: if(i_0-1)-1 ) temp = Successor-si_0j_0; Successor-si_0j_0 = Successor-si_0-1j_0; Successor-s
8、i_0-1j_0 = temp; return 1; else return 0; case 1: if(j_0-1)-1) temp = Successor-si_0j_0; Successor-si_0j_0 = Successor-si_0j_0-1; Successor-si_0j_0-1 = temp; return 1; else return 0; case 2: if( (j_0+1)si_0j_0; Successor-si_0j_0 = Successor-si_0j_0+1; Successor-si_0j_0+1 = temp; return 1; else retur
9、n 0; case 3: if(i_0+1)si_0j_0; Successor-si_0j_0 = Successor-si_0+1j_0; Successor-si_0+1j_0 = temp; return 1; else return 0; /-/从OPen表获取最佳节点函数入口/-struct Node * get_BESTNODE(struct Node *Open) return Open-next;/-/输出最佳路径函数入口/-void print_Path(struct Node * head) struct Node *q, *q1,*p; int i,j,count=1;
10、 p = (struct Node *)malloc(sizeof(struct Node); /通过头插法变更节点输出次序 p-previous = NULL; q = head; while(q) q1 = q-previous; q-previous = p-previous; p-previous = q; q = q1; q = p-previous; while(q) if(q = p-previous)printf(八数码的初始状态:n); else if(q-previous = NULL)printf(八数码的目标状态:n); else printf(八数码的中间态%dn,c
11、ount+); for(i=0; i3; i+) for(j=0; jsij); if(j = 2)printf(n); printf(f=%d, g=%dnn,q-f,q-g); q = q-previous; /-/A*子算法入口:处理后继结点/-void sub_A_algorithm(struct Node * Open, struct Node * BESTNODE, struct Node * Closed,struct Node *Successor) struct Node * Old_Node = (struct Node *)malloc(sizeof(struct Nod
12、e); Successor-previous = BESTNODE;/建立从successor返回BESTNODE的指针 Successor-g = BESTNODE-g + 1;/计算后继结点的g值/检查后继结点是否已存在于Open和Closed表中,如果存在:该节点记为old_Node,比较后继结点的g值和表中old_Node节点/g值,前者小代表新的路径比老路径更好,将Old_Node的父节点改为BESTNODE,并修改其f,g值,后者小则什么也不做。/即不存在Open也不存在Closed表则将其加入OPen表,并计算其f值 if( exit_Node(Open, Successor-s
13、, Old_Node) ) if(Successor-g g) Old_Node-next-previous = BESTNODE;/将Old_Node的父节点改为BESTNODE Old_Node-next-g = Successor-g;/修改g值 Old_Node-next-f = Old_Node-g + wrong_sum(Old_Node-s);/修改f值 /排序 del_Node(Open, Old_Node); Add_Node(Open, Old_Node); else if( exit_Node(Closed, Successor-s, Old_Node) if(Succe
14、ssor-g g) Old_Node-next-previous = BESTNODE; Old_Node-next-g = Successor-g; Old_Node-next-f = Old_Node-g + wrong_sum(Old_Node-s); /排序 del_Node(Closed, Old_Node); Add_Node(Closed, Old_Node); else Successor-f = Successor-g + wrong_sum(Successor-s); Add_Node(Open, Successor); open_N+;/-/A*算法入口/八数码问题的启发
15、函数为:f(n)=d(n)+p(n)/其中A*算法中的g(n)根据具体情况设计为d(n),意为n节点的深度,而h(n)设计为p(n),/意为放错的数码与正确的位置距离之和/-void A_algorithm(struct Node * Open, struct Node * Closed) /A*算法 int i,j; struct Node * BESTNODE, *inital, * Successor; inital = (struct Node * )malloc(sizeof(struct Node); /初始化起始节点 for(i=0; i3; i+) for(j=0; jsij
16、= inital_sij; inital-f = wrong_sum(inital_s); inital-g = 0; inital-previous = NULL; inital-next = NULL; Add_Node(Open, inital);/把初始节点放入OPEN表 open_N+; while(1) if(open_N = 0)printf(failure!); return; else BESTNODE = get_BESTNODE(Open);/从OPEN表获取f值最小的BESTNODE,将其从OPEN表删除并加入CLOSED表中 del_Node(Open, BESTNO
17、DE); open_N-; Add_Node(Closed, BESTNODE); if(equal(BESTNODE-s, final_s) /判断BESTNODE是否为目标节点 printf(success!n); print_Path(BESTNODE); return; /针对八数码问题,后继结点Successor的扩展方法:空格(二维数组中的0)上下左右移动, /判断每种移动的有效性,有效则转向A*子算法处理后继节点,否则进行下一种移动 else Successor = (struct Node * )malloc(sizeof(struct Node); Successor-nex
18、t = NULL; if(get_successor(BESTNODE, 0, Successor)sub_A_algorithm( Open, BESTNODE, Closed, Successor); Successor = (struct Node * )malloc(sizeof(struct Node); Successor-next = NULL; if(get_successor(BESTNODE, 1, Successor)sub_A_algorithm( Open, BESTNODE, Closed, Successor); Successor = (struct Node
19、* )malloc(sizeof(struct Node); Successor-next = NULL; if(get_successor(BESTNODE, 2, Successor)sub_A_algorithm( Open, BESTNODE, Closed, Successor); Successor = (struct Node * )malloc(sizeof(struct Node); Successor-next = NULL; if(get_successor(BESTNODE, 3, Successor)sub_A_algorithm( Open, BESTNODE, C
20、losed, Successor); /-/main()函数入口/定义Open和Closed列表。Open列表:保存待检查节点。Closed列表:保存不需要再检查的节点/-void main() struct Node * Open = (struct Node * )malloc(sizeof(struct Node); struct Node * Closed = (struct Node * )malloc(sizeof(struct Node); Open-next = NULL ; Open-previous = NULL; Closed-next =NULL; Closed-previous = NULL; A_algorithm(Open, Closed);时间:2021.02.07命题人:欧阳物
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1