九宫图C语言程序实现加注释.doc
上传人:sy****28 上传时间:2024-09-11 格式:DOC 页数:5 大小:44KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

九宫图C语言程序实现加注释.doc

九宫图C语言程序实现加注释.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

#include<stdio.h>#include<malloc.h>struct_State{intdata[3][3];introw;//行数intcol;//列数};声明一个结构体,定义一个整型的三行三列的二维数组,代表九宫图的九块图形。行和列分别为整型变量struct_StateList{struct_Statestate;intaction;intdepth;struct_StateList*next,*pre;};此结构体记录九宫图状态,怎么走,走了多少步,下一个节点,上一个节点char*action[5]={"","Blankgoup","Blankgodown","Blankgoleft","Blankgoright"};有五种走法:不走,向上,向下,向左,向右intobjstate[3][3]={{1,2,3},{8,0,4},{7,6,5}};目的状态intmaxdepth=10;最多可以走10步,以免陷入死循环struct_StateList*goup(struct_StateList*p){struct_StateList*temp;p->action=1;temp=(struct_StateList*)malloc(sizeof(struct_StateList));temp->state=p->state;temp->state.data[temp->state.row][temp->state.col]=temp->state.data[temp->state.row-1][temp->state.col];temp->state.data[temp->state.row-1][temp->state.col]=0;在向上走时,空白的行值减一,列值不变。此语句用赋值的方式代表向上走了一步。temp->state.row--;九宫图中空白每向上走一次,行值就减一temp->action=0;temp->depth=p->depth+1;每走一次都要使depth加一。temp->next=NULL;temp->pre=p;p->next=temp;printf("%s\n",action[p->action]);returntemp;}向上走的函数。此步骤在action数组下标为一。struct_StateList*godown(struct_StateList*p){struct_StateList*temp;p->action=2;temp=(struct_StateList*)malloc(sizeof(struct_StateList));temp->state=p->state;temp->state.data[temp->state.row][temp->state.col]=temp->state.data[temp->state.row+1][temp->state.col];temp->state.data[temp->state.row+1][temp->state.col]=0;temp->state.row++;temp->action=0;temp->depth=p->depth+1;temp->next=NULL;temp->pre=p;p->next=temp;printf("%s\n",action[p->action]);returntemp;}向下走,与向上走类似,只是行值是每走一步就会加一。struct_StateList*goleft(struct_StateList*p){struct_StateList*temp;p->action=3;temp=(struct_StateList*)malloc(sizeof(struct_StateList));temp->state=p->state;temp->state.data[temp->state.row][temp->state.col]=temp->state.data[temp->state.row][temp->state.col-1];temp->state.data[temp->state.row][temp->state.col-1]=0;temp->state.col--;temp->action=0;temp->depth=p->depth+1;temp->next=NULL;temp->pre=p;p->next=temp;printf("%s\n",action[p->action]);returntemp;