八数码问题.docx
- 文档编号:8212180
- 上传时间:2023-01-29
- 格式:DOCX
- 页数:14
- 大小:133.94KB
八数码问题.docx
《八数码问题.docx》由会员分享,可在线阅读,更多相关《八数码问题.docx(14页珍藏版)》请在冰豆网上搜索。
八数码问题
#include
#include
#include
#defineBOUND3
#defineINIT_LIST_SIZE20
#defineINCREMENT_LIST_SIZE5
#defineINIT_STACK_SIZE50
#defineINCREMENT_STACK_SIZE10
typedefstruct_Node
{
intnum[BOUND][BOUND];//记录状态
inth;//h(x)
struct_Node*parent;//父节点
}Node;
typedefNodeElemType;
typedefstruct_List
{
ElemType*elem;
intlength;
intlistsize;
}List,*PList;
typedefstruct_Stack
{
ElemType*base;
ElemType*top;
intstacksize;
}Stack,*PStack;
intmove[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//方向
voidInit(ElemType*,List*);//输入初始和最终状态
voidInit_List(PList);//初始化链表
intIsEmpty_List(PList);//判断链表是否为空
voidAdd_List(PList,ElemType*);//向链表中插入一个元素
voidRemove_List(PList,ElemType*,int);//从链表中取出一个元素
intHasElem(PList,ElemType*);//判断链表中是否有某个元素
voidSort_List(PList);//根据h对链表进行排序
voidDestroy_List(PList);//删除链表
voidInit_Stack(PStack);//初始化栈
intIsEmpty_Stack(PStack);//判断栈是否为空
voidPush(PStack,ElemType*);//压栈
voidPop(PStack,ElemType*);//出栈
voidDestroy_Stack(PStack);//删除栈
intGeth(int[][BOUND]);//根据状态计算h
intExpand(PList,PList,ElemType*);//根据一个节点进行扩展
voidShow_Node(Node*);//显示节点信息
voidAssign_Node(Node*,Node*);//节点复制
intEqual_Node(Node*,Node*);//判断两个节点的状态是否相等
ElemType*last;
intmain()
{
Listopen,close;
Stackstack;
ElemType*N=NULL;
intflag=0;
Init_List(&open);
Init_List(&close);
Init(N,&open);
while
(1)
{
if(IsEmpty_List(&open))
{
printf("搜索失败!
\n");
break;
}
N=(ElemType*)malloc(sizeof(ElemType));
Remove_List(&open,N,1);
Add_List(&close,N);
if(Equal_Node(last,N))
{
printf("搜索成功!
\n");
flag=1;
break;
}
else
{
if(Expand(&open,&close,N)==0)
continue;
Sort_List(&open);
}
}
Sleep(2000);
system("cls");
if(1==flag)
{
Init_Stack(&stack);
while
(1)
{
Push(&stack,N);
if(N->parent==NULL)
{
break;
}
N=N->parent;
}
while(!
IsEmpty_Stack(&stack))
{
system("cls");
printf("路径显示:
\n");
Pop(&stack,N);
Show_Node(N);
Sleep(1000);
}
Destroy_Stack(&stack);
}
Destroy_List(&open);
Destroy_List(&close);
return0;
}
voidInit(ElemType*N,List*open)
{
inti,j;
printf("输入初始状态:
\n");
N=(ElemType*)malloc(sizeof(ElemType));
for(i=0;i { for(j=0;j { scanf("%d",&N->num[i][j]); } } printf("输入最终状态: \n"); last=(ElemType*)malloc(sizeof(ElemType)); for(i=0;i { for(j=0;j { scanf("%d",&last->num[i][j]); } } N->h=Geth(N->num); N->parent=NULL; Add_List(open,N); } voidInit_List(PListplist) { plist->elem=(ElemType*)malloc(INIT_LIST_SIZE*sizeof(ElemType)); plist->listsize=INIT_LIST_SIZE; plist->length=0; } intIsEmpty_List(PListplist) { if(plist->length==0) return1; return0; } voidAdd_List(PListplist,ElemType*e) { if(plist->length>=plist->listsize) { plist->elem=(ElemType*)realloc(plist->elem,(plist->listsize+INCREMENT_LIST_SIZE)*sizeof(ElemType)); plist->listsize+=INCREMENT_LIST_SIZE; } Assign_Node(&plist->elem[plist->length],e); plist->length++; } voidRemove_List(PListplist,ElemType*N,intindex) { intk; if(plist->length==0) { N=NULL; } else { Assign_Node(N,&plist->elem[index-1]); for(k=index;k { Assign_Node(&plist->elem[k-1],&plist->elem[k]); } plist->length--; } } voidSort_List(PListplist) { inti,j,flag; ElemTypetemp; for(i=plist->length-1;i>0;i--) { flag=0; for(j=0;j { if(plist->elem[j].h>plist->elem[j+1].h) { Assign_Node(&temp,&plist->elem[j]); Assign_Node(&plist->elem[j],&plist->elem[j+1]); Assign_Node(&plist->elem[j+1],&temp); flag=1; } } if(! flag) break; } } intHasElem(PListplist,ElemType*e) { inti,j,k,t; for(k=0;k { t=0; for(i=0;i { for(j=0;j { if(plist->elem[k].num[i][j]==e->num[i][j]) { t++; } } } if(t==BOUND*BOUND) { return1; } } return0; } voidDestroy_List(PListplist) { free(plist->elem); plist->length=plist->listsize=0; } voidInit_Stack(PStackpstack) { pstack->base=(ElemType*)malloc(INIT_STACK_SIZE*sizeof(ElemType)); pstack->top=pstack->base; pstack->stacksize=INIT_STACK_SIZE; } intIsEmpty_Stack(PStackpstack) { if(pstack->top==pstack->base) return1; return0; } voidPush(PStackpstack,ElemType*pelem) { if(pstack->top-pstack->base>=pstack->stacksize) { pstack->base=(ElemType*)realloc(pstack->base,(pstack->stacksize+INCREMENT_STACK_SIZE)*sizeof(ElemType)); pstack->top=pstack->base+pstack->stacksize; pstack->stacksize+=INCREMENT_STACK_SIZE; } Assign_Node(pstack->top,pelem); (pstack->top)++; } voidPop(PStackpstack,ElemType*pelem) { if(IsEmpty_Stack(pstack)) exit(0); Assign_Node(pelem,(pstack->top-1)); (pstack->top)--; } voidDestroy_Stack(PStackpstack) { free(pstack->base); pstack->top=NULL; pstack->stacksize=0; } intGeth(intnum[][BOUND]) { inti,j,h=BOUND*BOUND; for(i=0;i { for(j=0;j { if(num[i][j]==last->num[i][j]) { h--; } } } returnh; } intExpand(PListpopen,PListpclose,ElemType*e) { intdir,i,j,m,n,a,b,state,t; ElemType*temp; t=0; for(dir=0;dir<4;dir++) { temp=(ElemType*)malloc(sizeof(ElemType)); for(i=0;i { for(j=0;j { if(e->num[i][j]==0) { n=i; m=j; } temp->num[i][j]=e->num[i][j]; } } a=n+move[dir][0]; b=m+move[dir][1]; if(a<0||a>BOUND-1||b<0||b>BOUND-1) { continue; } state=temp->num[n][m]; temp->num[n][m]=temp->num[a][b]; temp->num[a][b]=state; if(! HasElem(popen,temp)&&! HasElem(pclose,temp)) { temp->parent=e; temp->h=Geth(temp->num); Add_List(popen,temp); t++; } } returnt; } voidShow_Node(Node*N) { inti,j; for(i=0;i { for(j=0;j { if(N->num[i][j]! =0) { printf("%3d",N->num[i][j]); } else { printf("%3c",''); } } printf("\n"); } } voidAssign_Node(Node*N1,Node*N2) { inti,j; for(i=0;i { for(j=0;j { N1->num[i][j]=N2->num[i][j]; } } N1->h=N2->h; N1->parent=N2->parent; } intEqual_Node(Node*N1,Node*N2) { inti,j; for(i=0;i { for(j=0;j { if(N1->num[i][j]! =N2->num[i][j]) { return0; } } } return1; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数码 问题
![提示](https://static.bdocx.com/images/bang_tan.gif)