数据结构毕业课程设计报告迷宫求解问题.docx
- 文档编号:27011596
- 上传时间:2023-06-25
- 格式:DOCX
- 页数:29
- 大小:27.85KB
数据结构毕业课程设计报告迷宫求解问题.docx
《数据结构毕业课程设计报告迷宫求解问题.docx》由会员分享,可在线阅读,更多相关《数据结构毕业课程设计报告迷宫求解问题.docx(29页珍藏版)》请在冰豆网上搜索。
数据结构毕业课程设计报告迷宫求解问题
课题设计1:
迷宫求解
一.需求分析:
本程序是利用非递归的方法求出一条走出迷宫的路径,并将路径输出。
首先由用户输入一组二维数组来组成迷宫,确认后程序自动运行,当迷宫有完整路径可以通过时,以0和1所组成的迷宫形式输出,标记所走过的路径结束程序;当迷宫无路径时,提示输入错误结束程序。
二、概要设计:
1.抽象数据类型定义:
ADTFind{
数据对象:
D={ai?
ai∈ElemSet,i=1,2,…,n,n≥0}
数据关系:
R1={
ai-1,ai∈D}
基本操作:
find(&S)
初始条件:
已初始化栈S,且栈为空
操作结果:
从栈S中找出相对应的数据关系,并输出结果
}ADTFind
2.主程序的流程以及各程序模块之间的调用关系:
(1).定义变量i、j、w、z为整形变量
(2).输入迷宫二维数组maze(0:
m,0:
n)
(3).调用子程序find()
(4).结束程序
三、相应的源程序如下:
#include
#include
typedefenum{ERROR,OK}Status;
typedefstruct
{
introw,line;
}PosType;
typedefstruct
{
intdi,ord;
PosTypeseat;
}SElemType;
typedefstruct
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack&S);
StatusPush(SqStack&S,SElemType&a);
StatusPop(SqStack&S,SElemType&a);
StatusStackEmpty(SqStackS);
StatusMazePath(intmaze[12][12],SqStack&S,PosTypestart,PosTypeend);
voidInitmaze(intmaze[12][12],intsize);
voidprintmaze(intmaze[12][12],intsize);
StatusPass(intmaze[12][12],PosTypeCurPos);
voidMarkfoot(intmaze[12][12],PosTypeCurPos);
PosTypeNextPos(PosTypeCurPos,intDir);
voidprintpath(intmaze[12][12],SqStackS,intsize);
voidmain(void)
{
SqStackS;
intsize,maze[12][12];
for(intn=0;n<10;n++)
{
printf("创建一个正方形迷宫,请输入迷宫尺寸(注意不要大于50):
\n");
scanf("%d",&size);if(size<1||size>10){printf("输入错误!
");return;}
Initmaze(maze,size);
printmaze(maze,size);
PosTypestart,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);
elseprintf("找不到通路!
\n\n");
}
}
StatusMazePath(intmaze[12][12],SqStack&S,PosTypestart,PosTypeend)
{
PosTypecurpos;
intcurstep;
SElemTypee;
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)
returnOK;
curpos=NextPos(curpos,1);
curstep++;
}
else
{
if(!
StackEmpty(S))
{
Pop(S,e);
while(e.di==4&&!
StackEmpty(S))
{
Markfoot(maze,e.seat);
Pop(S,e);
}
if(e.di<4)
{
e.di++;
Push(S,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!
StackEmpty(S));
returnERROR;
}
voidInitmaze(intmaze[12][12],intsize)
{
charselect;
printf("选择创建方式A:
自动生成B:
手动创建\n");
label:
scanf("%c",&select);
if(select=='a'||select=='A')
{
for(inti=0;i for(i=1;i { maze[i][0]=1; for(intj=1;j maze[i][j]=rand()%2; maze[i][size+1]=1; } for(i=0;i } elseif(select=='b'||select=='B') { printf("按行输入%d*%d数据,0代表可通,1代表不可通(每行以Enter结束): \n",size,size); for(inti=0;i for(i=1;i { maze[i][0]=1; for(intj=1;j scanf("%d",&maze[i][j]); maze[i][size+1]=1; } for(i=0;i } elseif(select=='\n')gotolabel; elseprintf("输入错误! "); } voidprintmaze(intmaze[12][12],intsize) { printf("\n\n"); printf("显示所建的迷宫(#表示外面的墙): \n"); for(inti=0;i for(i=1;i { printf("%c",'#'); for(intj=1;j { printf("%d",maze[i][j]); } printf("%c",'#'); printf("\n"); } for(i=0;i } voidprintpath(intmaze[12][12],SqStackS,intsize) { printf("\n\n通路路径为: \n"); SElemType*p=S.base; while(p! =S.top) { maze[p->seat.row][p->seat.line]=2; p++; } for(inti=0;i for(i=1;i { printf("%c",'#'); for(intj=1;j { if(maze[i][j]==2)printf("%c",'0'); elseprintf(""); } printf("%c",'#'); printf("\n"); } for(i=0;i } StatusPass(intmaze[12][12],PosTypeCurPos) { if(maze[CurPos.row][CurPos.line]==0) returnOK; elsereturnERROR; } voidMarkfoot(intmaze[12][12],PosTypeCurPos) { maze[CurPos.row][CurPos.line]=1; } PosTypeNextPos(PosTypeCurPos,intDir) { PosTypeReturnPos; switch(Dir) { case1: ReturnPos.row=CurPos.row; ReturnPos.line=CurPos.line+1; break; case2: ReturnPos.row=CurPos.row+1; ReturnPos.line=CurPos.line; break; case3: ReturnPos.row=CurPos.row; ReturnPos.line=CurPos.line-1; break; case4: ReturnPos.row=CurPos.row-1; ReturnPos.line=CurPos.line; break; } returnReturnPos; } StatusInitStack(SqStack&S) { S.base=(SElemType*)malloc(100*sizeof(SElemType)); if(! S.base)returnERROR; S.top=S.base; S.stacksize=100; returnOK; } StatusPush(SqStack&S,SElemType&a) { *S.top++=a; returnOK; } StatusPop(SqStack&S,SElemType&a) { if(S.top==S.base)returnERROR; a=*--S.top; returnOK; } StatusStackEmpty(SqStackS) { if(S.top==S.base)returnOK; returnERROR; } 以下为测试数据: 输入一个矩阵,例如: 10011 00111 10001 01011 11000 输入入口行坐标和列坐标: 12 输入出口行坐标和列坐标: 55 通路路径为: 课题设计3: joseph环 一.需求分析: 利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。 首先创建一个空链表,初始化链表,构造出一个只有头结点的空链表,建立好一个约瑟夫环。 1.输入的形式和输入值的范围 本程序中,输入报数上限值m和人数上限l,密码,均限定为正整数,输入的形式为一个以“回车符”为结束标志的正整数。 2.输出的形式 从屏幕显示出列顺序。 3.程序功能 提供用户从键盘输入,Joseph约瑟夫环的必要数据,并显示出列顺序。 二、 概要设计 以单向循环链表实现该结构。 1.抽象数据类型的定义为: ADTLNode { 数据对象: D={ai|ai∈CharSet,i=1,2,…,n,n≥0} 数据关系: R1={<ai-1,ai>|ai∈D,I=2,…,n} 三.源程序: #include #include typedefstructNode { intkey;//每个人持有的密码 intnum;//这个人的编号 structNode*next;//指向下一个节点 }Node,*Link; voidInitList(Link&L)//创建一个空的链表 { L=(Node*)malloc(sizeof(Node)); if(! L)exit (1); L->key=0; L->num=0; L->next=L; } voidCreater(intn,Link&L)//初始化链表 { Linkp,q; q=L; for(inti=1;i<=n;i++) { p=(Node*)malloc(sizeof(Node)); if(! p)exit (1); printf("thekey_%dis: ",i); scanf("%d",&p->key); p->num=i; L->next=p; L=p; } L->next=q->next; free(q); } voidmain() { LinkL,p,q; intn,x; L=NULL; InitList(L);//构造出一个只有头结点的空链表 printf("pleaseinputthetotlenumberofpeople: "); scanf("%d",&n);//总共的人数n printf("thestartkeyis: "); scanf("%d",&x);//初始密码为x Creater(n,L);//建立好一个约瑟夫环 p=L; for(inti=1;i<=n;i++) { for(intj=1;j p=p->next; q=p->next; x=q->key; printf("%d",q->num); p->next=q->next; free(q); } } 四、测试数据: m的初值为20,n=7,7个人的密码依次为3,1,7,2,4,7,4 输出: 6741532 课题设计4: 商品货架管理 1、需求分析: 设计一个算法,每一次上货后始终保持生产日期越近的商品越靠近栈底。 求货架上剩余货物M、每天销售件数N、员工每天上货工作时间T,三者之间有何关系及T的最小值。 2、源程序: #include #include"string.h" #include"stdio.h" constintmaxsize=100; constintk=10; #defineelemtypechar typedefstruct { intMonth; intDay; intYear; }DATE; typedefstruct { intnum; DATEdate; }Node; classseqstack { public: Nodestack[maxsize]; inttop; voidinistack() { top=0; } voidpush(intx,intday,intmonth,intyear) { if(top==maxsize) cout<<"货架已满"< else { top++; stack[top].num=x; stack[top].date.Day=day; stack[top].date.Month=month; stack[top].date.Year=year; } } voidpop() { if(top==0) cout<<"货架已空"< else top--; } elemtypegettop() { if(top==0) cout<<"货架已空"< else returntop;} boolempty() { returntop==0; } }; voidmain() { seqstackc[k+1];//存放k种商品的数组,用c[0]来做中介货架 intTxq[k+1];//第i种取货用的时间 intTxs[k+1];//第i种上货用的时间 intNx[k+1];//第i种每天的销售数量 intN=0;//每天销售总量 intTx[k+1];//第i种每天上货的总时间 intT=0;//每天上货用的总时间 charyn='Y'; for(inti=1;i<=k;i++) { cout<<"******************************"< cout<<"商品货架管理系统"< cout<<"******************************"< Nodestore[20]; charyear,month; intcount;//货架上第i种商品的数目 intx,d,m,y;//x为第i种商品的序号 cout<<"请输入货架上第"< "< cout<<"(序号,生产日期(年、月、日如2006.2.13),现在货架上的存货数目,上货用时和取货用时)"< cin>>x>>y>>year>>m>>month>>d>>count>>Txs[i]>>Txq[i]; Nx[i]=maxsize-count; cout<<"货架上还需上"< Tx[k]=Txs[i]*(maxsize+count)+2*Txq[i]*count; cout<<"该货架满货需要用时"< cout<<"是否要上货? (Y/N)"< cin>>yn; if(yn=='Y'||yn=='y') { intnumbers,nian,yue,ri; cout<<"请输入要上货物的数目及生产日期(年、月、日)"< cin>>numbers>>nian>>yue>>ri; if(numbers>maxsize-count) { cout<<"要上的货物数目超出了货架的最大容量,请重新输入"< cin>>numbers>>nian>>yue>>ri; } for(intj=1;j<=numbers;j++) { } } N+=Nx[i]; T+=Tx[i]; } cout<<"每天销售总量为: "< cout<<"每天上货用的总时间为: "< } 3、测试结果: 五、课程设计5: 航空客运订票系统 1、需求分析: 对于本设计,可采用基数排序法对于一组具有结构特点的飞机航班号进行排序,利用二分查找法对排好序的航班记录按航班号实现快递查找,按其他次关键字的查找可采用最简单的顺序查找方法进行,因为它们用的较少。 2、源程序: #include #include #definemaxspace100 #definekeylen7 #defineradix_n10 #defineradix_c26 typedefcharkeytype; typedefstruct { charstart[6]; charend[6]; charsche[10]; chartime1[5]; chartime2[5]; charmodel[4]; intprice; }infotype; typedefstruct { keytypekeys[keylen]; infotypeothers; intnext; }slnode; typedefstruct { slnodesl[maxspace]; intkeynum; intlength; }sllist; typedefintarrtype_n[radix_n]; typedefintarrtype_c[radix_c]; voiddistribute(slnode*sl,inti,arrtype_nf,arrtype_ne) { intj,p; for(j=0;j { f[j]=e[j]=0; } for(p=sl[0].next;p;p=sl[p].next) { j=sl[p].keys[i]%48; if(! f[j]) f[j]=p; else sl[e[j]].next=p; e[j]=p; } } voidcollect(slnode*sl,inti,arrtype_nf,arrtype_ne) { intj,t; for(j=0;! f[j];j++); sl[0].next=f[j]; t=e[j]; while(j { for(j=j+1;j f[j];j++); if(f[j]) { sl[t].next=f[j]; t=e[j]; } } sl[t].next=0; } voiddistribute_c(slnode*sl,inti,arrtype_cf,arrtype_ce) { intj,p; for(j=0;j { f[j]=e[j]=0; } for(p=sl[0].next;p;p=sl[p].next) { j=sl[p].keys[i]%65; if(! f[j]) f[j]=p; else sl[e[j]].
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 毕业 课程设计 报告 迷宫 求解 问题