版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、迷宫源代码:#include <stdio.h>#include <slib.h>#include <malloc.h>#include <conio.h>#include <dos.h>#define ok 1#define error 0#define true 1#define false 0#define f9 0x43#define esc 0x1b#define del 0x53#define home 0x47#define end 0x4f#define space 0x20#define up 0x48#define
2、 down 0x50#define left 0x4b#define right 0x4d#define enter 0x0d#define f2 0x3c#define f3 0x3d#define stack_init_size 200#define stackincrement 10typedef int boolean;typedef int status;typedef suct int x;int y; postype;typedef suct int ord;postype seat;int di; selemtype;typedef suct int ;int foot;int
3、 mark; mazetype;typedef suct selemtype *base;selemtype *top;int stacksize; stack;int maze2030;mazetype maze2030;postype startplace;postype endplace;int count;int m,n;boolean b_start=false,b_end=false;void creatmaze(void);status savemaze(char *filename);status loadmaze(char *filename);void error(char
4、 *message);status initstack(stack *s);status desoystack(stack *s);status clearstack(stack *s);boolean stackempty(stack *s);int stacklength(stack *s);status push(stack *s,selemtype e);selemtype pop(stack *s,selemtype e);status gettop(stack *s,selemtype *e);status stacktraverse(stack *s,status (* visi
5、t)(selemtype *se);boolean pass(postype curpos);void markprint(postype seat);void footprint(postype curpos);postype nextpos(postype seat,int di);status mazepath(postype start,postype end);void creatmaze(void)/* form the maze. */void error(char *message);status savemaze(char *filename);status loadmaze
6、(char *filename);int i,j;int x,y;char c;char savename12,loadname12;boolean flag=false,load=false;clrscr();printf("menu:nn");printf("1.load mazefile:(*.dd)nn");printf("2.input maze:nn");printf("input your choice: ");doc=getch();switch(c)case ''''
7、;''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''
8、9;: putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''
9、;''''''); break;case ''''''''''''''''''''''''''''''''2'''''''''''''''''''
10、;''''''''''''': putch(''''''''''''''''''''''''''''''''2'''''''''''''
11、'''''''''''''''''''); break;case esc: sleep(1); exit(1);default: break;while(c!='''''''''''''''''''''''''''''
12、;'''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''
13、''''''''''2'''''''''''''''''''''''''''''''') ;if(c=''''''''''''''''&
14、#39;'''''''''''''''1'''''''''''''''''''''''''''''''')printf("nnloadname: ");scanf("%s",loadn
15、ame);if(loadmaze(loadname)sleep(1); load=true;else gotoxy(1,9); printf("load fail! "); if(!load)printf("ninput the maze''''''''''''''''''''''''''''''''s size
16、:n");printf("ninput length :n");scanf("%d",&m);printf("ninput width :n");scanf("%d",&n);if(m<4|n<4) error("input");if(m>30|n>20) error("maze too large");for(i=0;i<30;i )for(j=0;j<20;j )mazeji=2;startplace.x=0;
17、startplace.y=0;endplace.x=0;endplace.y=0;clrscr();br> for(i=1;i<=n;i )for(j=1;j<=m;j )printf(" #");mazei-1j-1=0;printf("n");gotoxy(65,5);printf("''''''''''''''''''''''''&
18、#39;'''''''#'''''''''''''''''''''''''''''''':wall");gotoxy(65,7);printf("start:home");gotoxy(65,9);printf("end:end");go
19、toxy(65,11);printf("delete wall:del");gotoxy(65,13);printf("enter wall:enter");gotoxy(65,15);printf("save maze:f2");gotoxy(65,17);printf("complete:f9");gotoxy(65,19);printf("exit:esc");gotoxy(4,3);x=4;y=3;doc=getch();switch(c)case up: if(y>3) y-;
20、gotoxy(x,y); break;case down: if(y<n) y ; gotoxy(x,y); break;case left: if(x>4) x-=2; gotoxy(x,y); break;case right: if(x<2*m-2) x =2; gotoxy(x,y); break;case del: if(y-2=startplace.y&&x/2-1=startplace.x) b_start=false;if(y-2=endplace.y&&x/2-1=endplace.x) b_end=false;putch(&
21、#39;''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''
22、''''); mazey-2x/2-1=1; gotoxy(x,y);break;case enter: if(y-2=startplace.y&&x/2-1=startplace.x) break;if(y-2=endplace.y&&x/2-1=endplace.x) break;putch(''''''''''''''''''''''''
23、;''''''''#''''''''''''''''''''''''''''''''); mazey-2x/2-1=0; gotoxy(x,y);break;case home: if(mazey-2x/2-1&&!b_start)startplace.x=x/2
24、-1;startplace.y=y-2;putch(''''''''''''''''''''''''''''''''s''''''''''''''''''''''
25、9;''''''''');gotoxy(x,y);b_start=true;break;case end: if(mazey-2x/2-1&&!b_end)endplace.x=x/2-1;endplace.y=y-2;putch(''''''''''''''''''''''''''''
26、''''e'''''''''''''''''''''''''''''''');gotoxy(x,y);b_end=true;break;case esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);case f9: if(b_start&
27、amp;&b_end) flag=true; break;case f2: gotoxy(2,22);printf("savename:");scanf("%s",savename);gotoxy(2,22);if(savemaze(savename) printf("save ok! ");else printf("save fail! ");sleep(1);gotoxy(2,22);printf(" ");gotoxy(x,y);break;default: break;while
28、(!flag);for(i=0;i<30;i )for(j=0;j<20;j )mazeji.=mazeji;mazeji.mark=0;mazeji.foot=0;status loadmaze(char *file)/* the maze has been loaded. */file *fp;char *buffer;char ch;int i=0,j,k;boolean len=false,wid=false;if(fp=fopen(file,"r")=null)return error;buffer=(char *)malloc(600*sizeof(
29、char);ch=fgetc(fp);while(ch!=eof)bufferi=ch;i ;ch=fgetc(fp);m=30;n=20;for(i=0;i<600;i )j=i/30; k=i0;if(bufferi=''''''''''''''''''''''''''''''''2'''''
30、'''''''''''''''''''''''''''&&!len) m=i; len=true; if(k=0&&bufferi=''''''''''''''''''''''
31、''''''''''2''''''''''''''''''''''''''''''''&&!wid) n=j; wid=true; switch(bufferi)case '''''''
32、9;''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': mazejk=0; break
33、;case ''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''
34、9;''''': mazejk=1; break;case ''''''''''''''''''''''''''''''''2''''''''''''''''''
35、;'''''''''''''': mazejk=2; break;case ''''''''''''''''''''''''''''''''3'''''''''
36、''''''''''''''''''''''': mazejk=1;startplace.x=k;startplace.y=j;b_start=true;break;case ''''''''''''''''''''''''
37、''''''''4'''''''''''''''''''''''''''''''': mazejk=1;endplace.x=k;endplace.y=j;b_end=true;break;default : break;fclose(fp);clrscr();for(i=0;i
38、<30;i )for(j=0;j<20;j )mazeji.=mazeji;mazeji.foot=0;mazeji.mark=0;if(mazeji=0)gotoxy(2*i 2,j 2);putch(''''''''''''''''''''''''''''''''#''''''&
39、#39;''''''''''''''''''''''''');gotoxy(2*startplace.x 2,startplace.y 2);putch(''''''''''''''''''''''''
40、9;'''''''s'''''''''''''''''''''''''''''''');gotoxy(2*endplace.x 2,endplace.y 2);putch('''''''''''
41、9;''''''''''''''''''''e'''''''''''''''''''''''''''''''');return ok;status savemaze(char *fil
42、ename)/* the maze has been saved. */file *fp;char *buffer;int i,j,k;fp=fopen(filename,"wb");buffer=(char *)malloc(600*sizeof(char);for(i=0;i<600;i )j=i/30; k=i0;switch(mazejk)case 0: bufferi='''''''''''''''''''&
43、#39;''''''''''''0'''''''''''''''''''''''''''''''' break;case 1: bufferi=''''''''''
44、39;'''''''''''''''''''''1'''''''''''''''''''''''''''''''' break;case 2: bufferi='
45、9;''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''
46、39;'' break;default : error("write"); break;if(k=startplace.x&&j=startplace.y) bufferi=''''''''''''''''''''''''''''''''3'''''&
47、#39;''''''''''''''''''''''''''if(k=endplace.x&&j=endplace.y) bufferi=''''''''''''''''''''''''
48、;''''''''4''''''''''''''''''''''''''''''''fwrite(buffer,600,1,fp);free(buffer);fclose(fp);return ok;void error(char *message)clrscr();fprint
49、f(serr,"error:%sn",message);exit(1); /* error */status initstack(stack *s)/* the stack s has been created and is initialized to be empty. */s->base=(selemtype *)malloc(stack_init_size*sizeof(selemtype);if(!s->base) error("overflow");s->top=s->base;s->stacksize=stac
50、k_init_size;return ok; /* initstack */status desoystack(stack *s)/* the stack s has been desoyed. */s->top=null;s->stacksize=0;free(s->base);s->base=null;return ok; /* desoystack */status clearstack(stack *s)/* the stack has been clear to be maximum. */s->top=s->base;s->stacksiz
51、e=stack_init_size;return ok; /* clearstack */boolean stackempty(stack *s)/* check if the stack s is empty. */if(s->top=s->base) return true;else return false; /* stackempty */int stacklength(stack *s)/* gain the length of the stack s. */if(s->top>s->base) return (int)(s->top-s->
52、base);else return 0; /* stacklength */status push(stack *s,selemtype e)/* the element e has been pushed into the stack s. */if(s->top-s->base>=s->stacksize)s->base=(selemtype *)realloc(s->base,(s->stacksize stackincrement)*sizeof(selemtype);if(!s->base) error("overflow&q
53、uot;);s->top=s->base s->stacksize;s->stacksize =stackincrement;*s->top =e;return ok; /* push */selemtype pop(stack *s,selemtype e)/* the element e has been removed from the stack s. */if(s->top=s->base) error("pop");e=*-s->top;return e; /* pop */status gettop(stack
54、*s,selemtype *e)/* the element e has got to the top of the stack s.*/if(s->top=s->base) error("gettop");*e=*(s->top-1);return ok; /* gettop */* traverse the stack s using ''''''''''''''''''''''
55、''''''''''visiting'''''''''''''''''''''''''''''''' function. */* status stacktraverse(stack *s,status (* visit)(selemtype *se)selemt
56、ype p;int result;if(s->top=s->base) return error;p=s->base;while(!(p=s->top)result=(*visit)(p);p ;return ok; */boolean pass(postype curpos)/* check if the current position can be passed. */if(mazecurpos.xcurpos.y.=1&&mazecurpos.xcurpos.y.foot=0&&mazecurpos.xcurpos.y.mark=
57、0)return true;else return false; /* pass */void markprint(postype seat)/* mark the position seat. */mazeseat.xseat.y.mark=-1;/* marking ''''''''''''''''''''''''''''''''-1
58、39;''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */ /* markprint */void footprint(postype curpos)/* the foot of the curpos of the maze has been
59、set ''''''''''''''''''''''''''''''''ue'''''''''''''''''''''''''''
60、;'''''. */mazecurpos.xcurpos.y.foot=1; /* footprint */postype nextpos(postype seat,int di)switch(di)case 1: seat.y ; return seat; /* eastward */case 2: seat.x ; return seat; /* southward */case 3: seat.y-; return seat; /* westward */case 4: seat.x-; return seat; /* northward */de
61、fault: seat.x=0; seat.y=0; return seat; /* nextpos */* the key to the program. */* pre: the maze array & the startplace & the endplace.post: find the one averse of the maze and perform the mazepath.uses: the adt stack class.*/status mazepath(postype start,postype end)postype curpos;int curst
62、ep;selemtype e;stack *s,stack;stack.base=(selemtype *)malloc(stack_init_size*sizeof(selemtype);if(!stack.base) error("overflow");stack.top=stack.base;stack.stacksize=stack_init_size;s=&stack;curpos=start;cursteprintf("n");<'0'''''''''
63、;''''''''''''''''''''''' break;case 1: bufferi=''''''''''''''''''''''''''''''''1
64、'''''''''''''''''''''''''''''''' break;case 2: bufferi='''''''''''''''''''''''
65、39;''''''''2'''''''''''''''''''''''''''''''' break;default : error("write"); break;if(k=startplace.x&&j=startplace.y) buff
66、eri=''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''
67、'''''if(k=endplace.x&&j=endplace.y) bufferi=''''''''''''''''''''''''''''''''4'''''''''''''
68、39;''''''''''''''''''fwrite(buffer,600,1,fp);free(buffer);fclose(fp);return ok;void error(char *message)clrscr();fprintf(serr,"error:%sn",message);exit(1); /* error */status initstack(stack *s)/* the stack s has been c
69、reated and is initialized to be empty. */s->base=(selemtype *)malloc(stack_init_size*sizeof(selemtype);if(!s->base) error("overflow");s->top=s->base;s->stacksize=stack_init_size;return ok; /* initstack */status desoystack(stack *s)/* the stack s has been desoyed. */s->top
70、=null;s->stacksize=0;free(s->base);s->base=null;return ok; /* desoystack */status clearstack(stack *s)/* the stack has been clear to be maximum. */s->top=s->base;s->stacksize=stack_init_size;return ok; /* clearstack */boolean stackempty(stack *s)/* check if the stack s is empty. */
71、if(s->top=s->base) return true;else return false; /* stackempty */int stacklength(stack *s)/* gain the length of the stack s. */if(s->top>s->base) return (int)(s->top-s->base);else return 0; /* stacklength */status push(stack *s,selemtype e)/* the element e has been pushed into
72、the stack s. */if(s->top-s->base>=s->stacksize)s->base=(selemtype *)realloc(s->base,(s->stacksize stackincrement)*sizeof(selemtype);if(!s->base) error("overflow");s->top=s->base s->stacksize;s->stacksize =stackincrement;*s->top =e;return ok; /* push *
73、/selemtype pop(stack *s,selemtype e)/* the element e has been removed from the stack s. */if(s->top=s->base) error("pop");e=*-s->top;return e; /* pop */status gettop(stack *s,selemtype *e)/* the element e has got to the top of the stack s.*/if(s->top=s->base) error("get
74、top");*e=*(s->top-1);return ok; /* gettop */* traverse the stack s using ''''''''''''''''''''''''''''''''visiting''''''''''&
75、#39;''''''''''''''''''''' function. */* status stacktraverse(stack *s,status (* visit)(selemtype *se)selemtype p;int result;if(s->top=s->base) return error;p=s->base;while(!(p=s->top)result=(*visit)(p);p
76、;return ok; */boolean pass(postype curpos)/* check if the current position can be passed. */if(mazecurpos.xcurpos.y.=1&&mazecurpos.xcurpos.y.foot=0&&mazecurpos.xcurpos.y.mark=0)return true;else return false; /* pass */void markprint(postype seat)/* mark the position seat. */mazeseat.
77、xseat.y.mark=-1;/* marking ''''''''''''''''''''''''''''''''-1''''''''''''''''''''''&
78、#39;''''''''' symbolize the current position cannot be put. */ /* markprint */void footprint(postype curpos)/* the foot of the curpos of the maze has been set '''''''''''''''''''''
79、'''''''''''ue''''''''''''''''''''''''''''''''. */mazecurpos.xcurpos.y.foot=1; /* footprint */postype nextpos(postype seat,int di)switch(di)case 1: seat.y ; return seat; /* eastward */case 2: seat.x ; return seat; /* southward */case 3: seat.y-; return seat; /* west
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年云南省蒙自市高二生物下册期末考试考试卷及答案(夺冠系列)
- 2026年山东省栖霞市高二生物下册期末考试检测卷含完整答案(全优)
- 2026年山东省肥城市高二生物下册期末考试测试卷附完整答案【夺冠】
- 2026中国邮政集团有限公司临沂市分公司社会招聘笔试参考题库及答案详解
- 2026年哈尔滨铁路分局哈尔滨医院医护人员招聘笔试备考题库及答案解析
- 2026年冷水江市中医医院医护人员招聘笔试备考题库及答案解析
- 2026中国煤炭地质总局校园招聘笔试模拟试题及答案详解
- 2025年东风轮胎集团公司职工医院医护人员招聘笔试题库及答案详解
- 2025年阜新市矿务局精神病医院医护人员招聘笔试试题及答案详解
- 2025年怀化医专附属医院怀化市第三人民医院医护人员招聘笔试试题及答案详解
- 2026年江苏省医疗系统事业编乡村医生人员招聘考试备考试题及答案详解
- 2026湖南省博物馆公开招聘考试备考试题及答案解析
- 初中生夏季高温中暑预防及急救全攻略
- 2026年医院编制考试公共基础知识综合能力题库(含答案)
- 2026年高考(广东卷)语文试题及答案
- 教科版三年级科学下册第三单元第6课《一天中影子的变化》教学设计
- 新疆哈密十三师水务管理有限公司招聘笔试题库2026
- 2026年重庆八中初升高中考自主招生英语试卷真题
- 防治艾滋病宣传课件
- 《Linux操作系统基础》课件-LVM逻辑卷的创建
- 2025届哈尔滨市重点中学新初三摸底考试英语试题含答案
评论
0/150
提交评论