885191311数据结构课程设计报告—迷宫求解问题.doc
《885191311数据结构课程设计报告—迷宫求解问题.doc》由会员分享,可在线阅读,更多相关《885191311数据结构课程设计报告—迷宫求解问题.doc(21页珍藏版)》请在三一办公上搜索。
1、课题设计1:迷宫求解一. 需求分析:本程序是利用非递归的方法求出一条走出迷宫的路径,并将路径输出。首先由用户输入一组二维数组来组成迷宫,确认后程序自动运行,当迷宫有完整路径可以通过时,以0和1所组成的迷宫形式输出,标记所走过的路径结束程序;当迷宫无路径时,提示输入错误结束程序。二、 概要设计:1.抽象数据类型定义:ADT Find数据对象:D=ai?ai ElemSet,i=1,2,n,n0数据关系:R1=?ai-1, aiD 基本操作: find (&S)初始条件:已初始化栈S,且栈为空操作结果:从栈中找出相对应的数据关系,并输出结果ADT Find2. 主程序的流程以及各程序模块之间的调用
2、关系:(1).定义变量i、j、w、z为整形变量(2).输入迷宫二维数组maze(0:m,0:n)(3).调用子程序find ()(4).结束程序三、相应的源程序如下:#include#includetypedef enum ERROR, OK Status;typedef struct int row, line; PosType; typedef struct int di, ord; PosType seat; SElemType; typedef structSElemType * base;SElemType * top;int stacksize;SqStack;Status Ini
3、tStack(SqStack &S); Status Push(SqStack &S,SElemType &a); Status Pop(SqStack &S,SElemType &a); Status StackEmpty(SqStack S); Status MazePath(int maze1212,SqStack &S, PosType start, PosType end); void Initmaze(int maze1212,int size); void printmaze(int maze1212,int size); Status Pass(int maze1212,Pos
4、Type CurPos); void Markfoot(int maze1212, PosType CurPos); PosType NextPos(PosType CurPos, int Dir); void printpath(int maze1212,SqStack S,int size);void main (void) SqStack S;int size,maze1212; for(int n=0;n10;n+) printf(创建一个正方形迷宫,请输入迷宫尺寸(注意不要大于50):n); scanf(%d,&size);if(size10)printf(输入错误!);return
5、; Initmaze(maze,size); printmaze(maze,size); PosType start,end; printf(输入入口行坐标和列坐标:);scanf(%d,&start.row);scanf(%d,&start.line); printf(输入出口行坐标和列坐标:);scanf(%d,&end.row);scanf(%d,&end.line); if(MazePath(maze,S,start,end) printpath(maze,S,size); else printf(找不到通路!nn);Status MazePath(int maze1212,SqSta
6、ck &S, PosType start, PosType end) PosType curpos;int curstep;SElemType e;InitStack(S);curpos = start; curstep = 1; do if (Pass(maze,curpos) Markfoot(maze,curpos); e.di =1; e.ord = curstep; e.seat= curpos; Push(S,e); if (curpos.row=end.row & curpos.line=end.line) return OK; curpos = NextPos(curpos,
7、1); curstep+; else if (!StackEmpty(S) Pop(S,e); while (e.di=4 & !StackEmpty(S) Markfoot(maze,e.seat); Pop(S,e); if (e.di4) e.di+; Push(S, e); curpos = NextPos(e.seat, e.di); while (!StackEmpty(S);return ERROR; void Initmaze(int maze1212,int size) char select; printf(选择创建方式 A:自动生成 B:手动创建n); label:sca
8、nf(%c,&select); if(select=a|select=A) for(int i=0;isize+2;i+)maze0i=1; for( i=1;isize+1;i+) mazei0=1; for(int j=1;jsize+1;j+) mazeij=rand()%2; mazeisize+1=1; for(i=0;isize+2;i+)mazesize+1i=1; else if(select=b|select=B) printf(按行输入%d*%d数据,0代表可通,1代表不可通(每行以Enter结束):n,size,size); for(int i=0;isize+2;i+)
9、maze0i=1; for( i=1;isize+1;i+) mazei0=1; for(int j=1;jsize+1;j+) scanf(%d,&mazeij); mazeisize+1=1; for(i=0;isize+2;i+)mazesize+1i=1; else if(select=n)goto label; else printf(输入错误!);void printmaze(int maze1212,int size)printf(nn);printf(显示所建的迷宫(#表示外面的墙):n); for(int i=0;isize+2;i+)printf(%c ,#);printf
10、(n);for(i=1;isize+1;i+) printf(%c ,#); for(int j=1;jsize+1;j+) printf(%d ,mazeij); printf(%c,#); printf(n); for(i=0;iseat.rowp-seat.line=2; p+; for(int i=0;isize+2;i+)printf(%c ,#);printf(n);for(i=1;isize+1;i+) printf(%c ,#); for(int j=1;jsize+1;j+) if(mazeij=2) printf(%c ,0); else printf( ); printf
11、(%c,#); printf(n); for(i=0;isize+2;i+)printf(%c ,#);printf(nn);Status Pass(int maze1212,PosType CurPos)if (mazeCurPos.rowCurPos.line=0) return OK; else return ERROR; void Markfoot(int maze1212,PosType CurPos)mazeCurPos.rowCurPos.line=1;PosType NextPos(PosType CurPos, int Dir) PosType ReturnPos; swit
12、ch (Dir) case 1: ReturnPos.row=CurPos.row; ReturnPos.line=CurPos.line+1; break; case 2: ReturnPos.row=CurPos.row+1; ReturnPos.line=CurPos.line; break; case 3: ReturnPos.row=CurPos.row; ReturnPos.line=CurPos.line-1; break; case 4: ReturnPos.row=CurPos.row-1; ReturnPos.line=CurPos.line; break;return R
13、eturnPos;Status InitStack(SqStack &S)S.base=(SElemType *)malloc(100*sizeof(SElemType);if(!S.base)return ERROR;S.top=S.base;S.stacksize=100;return OK;Status Push(SqStack &S,SElemType &a) *S.top+=a;return OK;Status Pop(SqStack &S,SElemType &a)if(S.top=S.base)return ERROR;a=*-S.top;return OK;Status Sta
14、ckEmpty(SqStack S)if(S.top=S.base)return OK;return ERROR;以下为测试数据:输入一个矩阵,例如:1 0 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0 0 0 输入入口行坐标和列坐标:1 2输入出口行坐标和列坐标:5 5通路路径为:课题设计3:joseph环一. 需求分析:利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。首先创建一个空链表,初始化链表,构造出一个只有头结点的空链表,建立好一个约瑟夫环。1. 输入的形式和输入值的范围 本程序中,输入报数上限值m和人数上限l,密码,均限定为
15、正整数,输入的形式为一个以“回车符”为结束标志的正整数。2. 输出的形式 从屏幕显示出列顺序。3. 程序功能 提供用户从键盘输入,Joseph约瑟夫环的必要数据,并显示出列顺序。二、 概要设计以单向循环链表实现该结构。1. 抽象数据类型的定义为:ADT LNode 数据对象:D=ai | aiCharSet,i= 1,2,n,n0 数据关系:R1=< ai-1 ,ai > | ai D, I=2,n三源程序:#include #include typedef struct Node int key;/每个人持有的密码 int num;/这个人的编号 struct Node *nex
16、t;/指向下一个节点 Node,*Link; void InitList(Link &L) /创建一个空的链表 L=(Node *)malloc(sizeof(Node); if(!L) exit(1); L-key=0; L-num=0; L-next=L; void Creater(int n,Link &L) /初始化链表 Link p,q; q=L; for(int i=1;ikey); p-num=i; L-next=p; L=p; L-next=q-next; free(q); void main() Link L,p,q; int n,x; L=NULL; InitList(L)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 885191311 数据结构 课程设计 报告 迷宫 求解 问题
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-2396413.html