2023年c语言迷宫问题代码实现.docx
2023年c语言迷宫问题代码实现 第一篇:c语言迷宫问题代码实现 C语言迷宫问题代码如下: #include #include #define LEN sizeof(SEAT) #define MAXSIZE 100 #define LENGTH 30 typedef struct int x;/横坐标 int y;/纵坐标 int di;/表示方向,0-3分别表示东南西北。 SEAT; struct StackList SEAT stack; int top; *Stack; int EmptyStack(StackList*Stack)/推断是否为空栈 if(Stack->top=0) return 0; else return 1; int Move=0,1,1,0,0,-1,-1,0;/分别表示向东、西、南、北需要加上的坐标 int Mase=0;/初始化为0 int length,width; void InitMase()/在迷宫的外围添加一层“墙壁赋值为1,使得迷宫的随便一点都有四个方向 int i,j; for(i=0;itop=0; return; int PushStack(StackList*Stack,SEAT CurSeat)/进栈 if(Stack->top=MAXSIZE-1) return false; else Stack->stack.x=CurSeat.x; Stack->stack.y=CurSeat.y; Stack->stack.di=CurSeat.di; Stack->top+; return true; int PopStack(StackList*Stack)/出栈 if(Stack->top=0) return false; else Stack->top-; return true; int Pass(SEAT p)/推断当前是否可行 if(Mase=0) return true; else return false; SEAT NextSeat(SEAT CurSeat)/查找下一个点,并返回 SEAT temp; temp.x=CurSeat.x+Move; temp.y=CurSeat.y+Move; return temp; void Mark(SEAT p)/标记已经走过的点,避开重复 Mase=-1; int PathWay(SEAT start,SEAT end)/找路途 Stack=(struct StackList*)malloc(sizeof(struct StackList); InitStack(Stack); SEAT CurSeat; CurSeat.x=start.x+1;/由于多加了一层墙壁,因此坐标的值要加1 CurSeat.y=start.y+1;/ CurSeat.di=start.di;/ do if(Pass(CurSeat) PushStack(Stack,CurSeat); Mark(CurSeat); if(CurSeat.x=end.x+1&&CurSeat.y=end.y+1)/假如找到出口,返回 return true; else int find=0; while(CurSeat.distack; while(CurSeat.di=3&&EmptyStack(Stack)/当前的点找不到下一个点,出栈 PopStack(Stack); CurSeat=Stack->stack;/当前的点变为前一个点 if(EmptyStack(Stack) int find=0; while(CurSeat.ditop=0) printf(“There is no route can be out of the mazen); else int i; for(i=0;itop;i+) if(i!=0) printf(“->(%d,%d),Stack->stack.x-1,Stack->stack.y-1); else printf(“(%d,%d),Stack->stack.x-1,Stack->stack.y-1); void PrintMase()/输出迷宫 int i,j; for(i=1;i=0 数据关系:R=|ai-1,ai属于D,i=2,3,n 基本操作: InitStack(&S) 操作结果:构造一个空栈 Push&S,e 初始条件:栈已经存在 操作结果:将e所指向的数据加入到栈s中 Pop&S,&e 初始条件:栈已经存在 操作结果:若栈不为空,用e返回栈顶元素,并删除栈顶元素 Getpop&S,&e 初始条件:栈已经存在 操作结果:若栈不为空,用e返回栈顶元 StackEmpty(&S) 初始条件:栈已经存在 操作结果:推断栈是否为空。若栈为空,返回1,否则返回0 Destroy(&S) 初始条件:栈已经存在 操作结果:销毁栈s ADT Stack 2设定迷宫的抽象数据类型定义 ADT yanshu 数据对象:D=ai,j|ai,j属于 、*、#,0=(*S).stacksize)/ 栈满,追加存储空间 *(*S).top)+=e;return 1; / 若栈不空,则删除S的栈顶元素,用e返回其值,并返回1;否则返回0。int Pop(SqStack *S,SElemType *e) if(*S).top =(*S).base) 左 return 1; / 定义墙元素值为0,可通过路径为1,不能通过路径为-1,通过路径为踪迹 / 当迷宫m的b点的序号为1(可通过路径),return 1;否则,return 0。int Pass(PosType b) if(m=1) else return 0;return 1;return 0;*e = *-(*S).top; / 这个等式的+ * 优先级相同,但是它们的运算方式,是自右向 (*S).base =(SElemType *)realloc(*S).base ,(*S).top =(*S).base+(*S).stacksize;(*S).stacksize += STACKINCREMENT;(*S).stacksize + STACKINCREMENT)* sizeof(SElemType);exit(0);if(!(*S).base)return 0;return 1; void FootPrint(PosType a) / 使迷宫m的a点的序号变为踪迹(curstep),表示经过 m=curstep; / 根据当前位置及移动方向,返回下一位置 PosType NextPos(PosType c,int di) PosType direc=0,1,1,0,0,-1,-1,0;/ 行增量,列增量 / 移动方向,依次为东南西北 c.x+=direc.x;c.y+=direc.y;return c; / 使迷宫m的b点的序号变为-1(不能通过的路径)void MarkPrint(PosType b) m=-1; / 若迷宫maze中存在从入口start到出口end的通道,则求得一条 / 存放在栈中(从栈底到栈顶),并返回1;否则返回0 int MazePath(PosType start,PosType end) SqStack S;PosType curpos;SElemType e; InitStack(&S);curpos=start;do if(Pass(curpos)/ 当前位置可以通过,即是未曾走到过的通道块 FootPrint(curpos);/ 留下踪迹 e.ord=curstep;e.seat.x=curpos.x;e.seat.y=curpos.y;e.di=0;Push(&S,e);/ 入栈当前位置及状态 curstep+;/ 踪迹加1 if(curpos.x=end.x&&curpos.y=end.y)/ 到达终点(出口) else return 1;curpos=NextPos(curpos,e.di);/ 当前位置不能通过 if(!StackEmpty(S) Pop(&S,&e);/ 退栈到前一位置 curstep-;while(e.di=3&&!StackEmpty(S)/ 前一位置处于最终一个方向(北) if(e.disemester>p1->semester)&&(p1->next) p2=p1; p1=p1->next; if(p0->semester semester) if(*head=p1)*head=p0; else p2->next=p0; p0->next=p1; else if(p0->semester=p1->semester) while(p0->cID>p1->cID)&&(p1->next)&&(p0->semester=p1->semester) if(p0->semester!=p1->semester) else if(p0->cIDcID) if(*head=p1)*head=p0;else p2->next=p0;p2=p1;p1=p1->next;p2->next=p0;p0->next=p1; p0->next=p1; else p1->next=p0;p0->next=NULL; else p1->next=p0;p0->next=NULL; else *head=p0; p0->next=NULL; void Print(struct course *head,int *n) struct course *p;p=*head;if(*head) if(*n=1)printf(“nThis %d record is:n,*n); else printf(“nThese %d records are:n,*n); printf(“semester cID name creditn); do printf(“%-10d%-10d%-18s%-12.1f n,p->semester,p->cID,p->name,p->credit); p=p->next; while(p!=NULL); else printf(“nList null!n); void Modify(struct course *head,int *n) struct course *p,*p2;int cID;if(*head) Print(head,n);while(1) printf(“nPlease input the cID which you want to modify:); scanf(“%d,&cID);p2=p=*head;while(p->next&&(cID!=p->cID) p2=p; p=p->next; if(cID=p->cID) printf(“Please input the new cID(160):); scanf(“%d,&p->cID); while(p->cIDcID>60) printf(“nError!); printf(“nPlease input the new cID(160):); scanf(“%d,&p->cID); printf(“Please input the new semester(18):); scanf(“%d,&p->semester);while(p->semestersemester>8) printf(“nError!); printf(“nPlease input the new semester(18):); scanf(“%d,&p->semester); printf(“Please input the new credit:); scanf(“%f,&p->credit); printf(“Please input the new name:); scanf(“%s,p->name); if(p=*head)*head=p->next; else p2->next=p->next; insert(head,p); break; else printf(“%d not been found!n,cID); else printf(“nList null!n); void Require(struct course *head) struct course *p;float sum=0;int sem,i=0;printf(“nPlease input the semester which is required:); scanf(“%d,&sem);p=*head;while(p) if(sem=p->semester) i+;if(i=1)printf(“nsemester cID name creditn);printf(“%-10d%-10d%-18s%-12.1f n,p->semester,p->cID,p->name,p->credit); sum=sum+p->credit; p=p->next; printf(“The sum of credit in this term is:%.1fn,sum); void Creat(struct course *head,int *n) struct course *p1;while(1) p1=(struct course *)malloc(LEN); printf(“Please input the cID(160):); scanf(“%d,&p1->cID); while(p1->cIDcID>60) printf(“nError!); printf(“nPlease input the cID(160):); scanf(“%d,&p1->cID); if(p1->cID=0)break; printf(“Please input the semester(18):); scanf(“%d,&p1->semester); while(p1->semestersemester>8) printf(“nError!); printf(“nPlease input the semester(18):);scanf(“%d,&p1->semester); printf(“Please input the credit:);scanf(“%f,&p1->credit);printf(“Please input the name:);scanf(“%s,p1->name);insert(head,p1);*n=*n+1;printf(“nYou can continue until the cID is 0“!n);Print(head,n);void Delete(struct course *head,int *n) struct course *p1,*p2;int cID;Print(head,n);if(*head) printf(“Please input the cID of the course which you want to delete:);scanf(“%d,&cID);p1=*head; while(cID!=p1->cID&&p1->next!=NULL) p2=p1; p1=p1->next; if(cID=p1->cID) if(p1=*head)*head=p1->next; else p2->next=p1->next; printf(“Have delete cID:%dn,cID); *n=*n-1; else printf(“%d not been found!n,cID); void Fun(struct course *head,int *n) char num; while(1) system(“cls); puts(“* Main Menu *); puts(“* 1.Add Records 2.Print Records *); puts(“* 3.Delete Records 4.Modify Records *); puts(“* 5.Require Records 6.Exit *); printf(“Please input your choice: ); scanf(“%d,&num); switch(num) case 1:Creat(head,n);break; case 2:Print(head,n);break; case 3:Delete(head,n);break; case 4:Modify(head,n);break; case 5:Require(head);break;case 6:exit(0);break; default: break; printf(“nPress Enter“ to continue!);getchar();getchar();