1、/以上为C+源文件using namespace std;static int space=0;int target9;class EightNum/定义一个EightNum类 public: int num9; int f;/初始状态与目标状态相比,棋子错放个数 int deap;/深度 int evalfun;/状态的估价值 EightNum *parent;/以下为类内成员函数的声明 EightNum(int nnum9); int get_evalfun(); int get_deapfun(); void eval_func(int id); int Canspread(int n)
2、; void Spreadchild(int n); void getnum(int num19); void setnum(int num19); void show(void); int operator =(EightNum& NewEightN); int operator =(int num29); int Shownum();/-以下为EightNum类成员函数定义-/class Stack private: EightNum * eightnum; public: Stack * next; EightNum * Minf(); EightNum * Belong(EightNu
3、m * suc); void Putinto(EightNum * suc);EightNum:EightNum(int nnum9)/此函数功能为:初始化num;for(int i=0;i9;i+) numi=nnumi;f=0;deap=0;parent=NULL;int EightNum:get_evalfun()return evalfun;get_deapfun() return deap;void EightNum:eval_func(int id)/此函数为估价函数int i,qifa;qifa=0;switch(id)case 1: for(i=0;i+) if(numi!=t
4、argeti) qifa+; break;case 2: int j, h1,h2; for(j=0;jparent=NULL) deap=0; else deap=this-parent-deap+1; evalfun=deap+f;Canspread(int n)/判断空格0可否移动 int i,flag = 0; for(i = 0;i numi = 0)break; switch(n) case 1: if(i/3 != 0)flag = 1; case 2:= 2)flag = 1; case 3: if(i%3 ! case 4: default: return flag ;Spr
5、eadchild(int n)/扩展child节点的子节点 int i,loc,qifa; this-numi = this-numi; 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; else loc = i%3+1+(i/3)*3; qifa = this-numloc; this-numi = qifa;numloc = 0;getnum(int num19) num1i=numi;setnum(int num19) numi=num1
6、i;show()/输出函数 coutnumi ; if(i+1)%3=0)n coutShownum();show(); coutendl; return n+1;operator =(EightNum& NewEightN)int compere=1; if(numi!=NewEightN.numi) compere=0;if(compere=0) return 0;else return 1;/-以下为分函数的定义-/判断是否有解的函数int solve(int num9,int target9)int i,j;int num_con=0,tar_con=0;for(i=0; for(j=
7、0;i; if(numjnumi & numj!=0) num_con+; if(targetjnext; Stack * min = this- Stack * minp = this; EightNum * minx; while(qifa-next != NULL) if(qifa-next-eightnum-get_evalfun() get_evalfun() min = qifa- minp = qifa; qifa = qifa- minx = min-eightnum; qifa = minp- minp-next = minp- free(qifa); return minx
8、;/判断节点是否属于OPEN表或CLOSED表Belong(EightNum * suc) Stack * qifa = this- next ; if(qifa = NULL)return NULL; while(qifa ! if(suc=qifa-eightnum)return qifa - return NULL;/把节点存入OPEN 或CLOSED 表中void Stack:Putinto(EightNum * suc) Stack * qifa; qifa =(Stack *) malloc(sizeof(Stack); qifa-eightnum = suc;next = thi
9、s-next = qifa;int BelongProgram(EightNum * suc ,Stack *Open ,Stack *Closed ,EightNum goal,int m ) EightNum * qifa = NULL; int flag = 0; if(Open-Belong(suc) != NULL) | (Closed-= NULL) if(Open-= NULL) qifa = Open-Belong(suc); else qifa = Closed- flag=1; Open-Putinto(suc); suc-eval_func(m); return flag
10、;/扩展后继节点总函数void Spread(EightNum * suc, Stack * Open, Stack * Closed, EightNum goal,int m) int i; EightNum * child; i Canspread(i+1) space+; child = (EightNum *) malloc(sizeof(EightNum); child-parent = suc;Spreadchild(i); if(BelongProgram(child, Open, Closed, goal,m) /判断子节点是否属于OPEN或CLOSED表 free(child
11、);/执行函数EightNum * Process(EightNum * org, EightNum goal, Stack * Open, Stack * Closed,int m) while(1) next = NULL)return NULL; EightNum * minf =Open-Minf(); Closed-Putinto(minf); if(*minf)=goal)return minf; Spread(minf, Open, Closed, goal,m); /-A*算法搜索函数-/void A(int id,EightNum start,EightNum Target)
12、 EightNum * result; space=0; float time; Stack *Open = (Stack *) malloc(sizeof(Stack); Open-next = NULL; Stack *Closed = (Stack *) malloc(sizeof(Stack); Closed- clock_t startt,finisht; startt=clock();/开始时间 start.eval_func(id);Putinto(&start); result = Process(&start, Target, Open, Closed,id); /进行剩余的
13、操作n搜索过程:result-Shownum() finisht=clock(); time=(float)(finisht-startt);endlidj+) if(numj=numi) flag=1; if(numi8|flag=1) error+; if(error!输入数据错误!请重新输入!while(error!=0);/输入八数码问题的初始状态(0代表空格,“棋子”间用空格隔开);int error1; error1=0;请输入新的目标状态(用0代表空格,“棋子”间用空格隔开):targeti; if(targetj=targeti) if(targeti9|flag=1) err
14、or1+; if(error1!while(error1!/输入八数码问题的目标状态(用0代表空格,中间用空格隔开);EightNum start(num),Target(target);int m=solve(num,target);/判断初始状态到目标状态是否有解,有解返回1,误解返回0;if(m=0)此状态无解!int id=0;while(id!=3)1. 错放的棋子个数为;n2.每个棋子与目标位置之间的距离总和为;3.结束,退出程序!n请选择功能,分别输入“1”“2”“3”进行选择:id; switch(id) 错放的棋子个数结果为:n(以下逐一展示搜索过程:) A(1,start,
15、Target); break;每个棋子与其目标位置之间的距离总和为: A(2,start,Target);啊啊.程序结束!实验截图实验中遇到的问题1:开始程序只能运行一种方式即按照错位个数搜索,后经过查找相关资料,修改后可程序可进行选择,两种方法结合在一起根据选择运行。 实验总结通过本次实验让我对八数码问题有了进一步的了解,也对一般图搜索和启发式搜索问题的解决有了更深的理解,启发式函数是通过考虑搜索算法的可采纳性,根据定义的评价函数选择最佳路径的一种方法,根据不同的函数可能得到不同的搜索路径,通过这次实验让我对这类游戏行的程序更加有兴趣,也让我的编程更加熟练和编程的思维更清晰了点,从而对学习编程更加有兴趣了。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1