迷宮問題課程設(shè)計_第1頁
迷宮問題課程設(shè)計_第2頁
迷宮問題課程設(shè)計_第3頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、    迷宮問題課程設(shè)計    目 錄 1問題描述.1 2需求分析 1 3概要設(shè)計 1 3.1抽象數(shù)據(jù)類型定義 1 3.2子程序及功能要求 1 3.3各程序模塊之間的調(diào)用關(guān)系 2 4詳細設(shè)計 2 4.1設(shè)計相應(yīng)數(shù)據(jù)結(jié)構(gòu) 2 4.2主要模塊的算法描述 3 5測試分析 9 6課程設(shè)計總結(jié) 10 參考文獻 10 附錄(源程序清單) 10    1 問題描述    編制程序給出一條通過迷宮的路徑或報告一個“無法通過”的信息。該迷宮是一個M行N列

2、的0-1矩陣,其中0表示無障礙,1表示有障礙。設(shè)入口為(1,1)出口為(M,N)每次移動只能從一個無障礙的單元移到其周圍8個方向上任一無障礙的單元, 2 需求分析 該算法的基本思想是: (1) 若當前位置“可通”,則納入路徑,繼續(xù)前進; (2)若當前位置“不可通”,則后退,換方向繼續(xù)探索; (3)若四周“均無通路”,則將當前位置從路徑中刪除出去。 3 概要設(shè)計 3.1 抽象數(shù)據(jù)類型定義 typedef struct int x, y; /坐標 int dir; /方向 ElemType; typedef struct StackNode/構(gòu)造棧 ElemType *base; ElemType

3、 *top; int stacksize; SqStack; 3.2 子程序及功能要求 (1)void Input(char bMM); (2)void Ouput(const char bMM); (3)int FindWay(char mazeMM); (4)int NextStep(int *x, int *y, int dir).    3.3 各程序模塊之間的調(diào)用關(guān)系 主函數(shù)可調(diào)用子程序void Input(char bMM); void Ouput(const char bMM); int FindWay(char mazeMM); int

4、NextStep(int *x, int *y, int dir). 4 詳細設(shè)計 4.1 設(shè)計相應(yīng)數(shù)據(jù)結(jié)構(gòu) (1)迷宮類型定義 typedef struct int x, y; /坐標 int dir; /方向 ElemType; typedef struct StackNode/構(gòu)造棧 ElemType *base; ElemType *top; int stacksize; SqStack; (2)棧元素類型定義 int InitStack(SqStack *S) /初始化棧 S-base=(ElemType*) malloc(STACK_INIT_SIZE*sizeof(ElemTyp

5、e); if(!S-base) printf("memory allocation failed,goodbye"); exit(1); S-top=S-base; S-stacksize=STACK_INIT_SIZE; return OK; 4.2 主要模塊的算法描述    #include #include #define M 10 /自己規(guī)定為10*10的迷宮 #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define STACK_INIT_SIZE 100 #define

6、 STACKINCREMENT 10 int findway(int); int NextStep(int *, int *, int ); typedef struct int x, y; /坐標 int dir; /方向 ElemType; typedef struct StackNode/構(gòu)造棧 ElemType *base; ElemType *top; int stacksize; SqStack; int InitStack(SqStack *S) /初始化棧 S-base=(ElemType*) malloc(STACK_INIT_SIZE*sizeof(ElemType); i

7、f(!S-base) printf("memory allocation failed,goodbye"); exit(1); S-top=S-base; S-stacksize=STACK_INIT_SIZE; return OK; int Push(SqStack *S,ElemType e) /進棧操作 if(S-top-S-base=S-stacksize) S-base=(ElemType*) realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(Eleme); if (!S-base) printf("m

8、emory allocation failed,goodbye"); exit(1); S-top = S-base+S-stacksize; S-stacksize += STACKINCREMENT; *S-top+=e; return OK; int Pop(SqStack *S,ElemType *e) /出棧操作 if(S-top=S-base) return ERROR; *e=*-S-top; printf("%dn",e); return OK; int StackEmpty(SqStack *S) /判斷棧是否為空 if(S-top=S-base

9、) return OK; else return ERROR; void Input(char bMM) /輸入時候請注意把一圈都輸入為墻即'#' int i, j; printf("qingshuirumigongxingzhuang:n"); for (i = 0; i #include #define M 10 /自己規(guī)定為10*10的迷宮 #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 int

10、findway(int); int NextStep(int *, int *, int ); typedef struct int x, y; /坐標 int dir; /方向 ElemType; typedef struct StackNode/構(gòu)造棧 ElemType *base; ElemType *top; int stacksize; SqStack; int InitStack(SqStack *S) /初始化棧 S-base=(ElemType*) malloc(STACK_INIT_SIZE*sizeof(ElemType); if(!S-base) printf("

11、;memory allocation failed,goodbye"); exit(1); S-top=S-base; S-stacksize=STACK_INIT_SIZE; return OK; int Push(SqStack *S,ElemType e) /進棧操作 if(S-top-S-base=S-stacksize) S-base=(ElemType*) realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(Eleme); if (!S-base) printf("memory allocation failed

12、,goodbye"); exit(1); S-top = S-base+S-stacksize; S-stacksize += STACKINCREMENT; *S-top+=e; return OK; int Pop(SqStack *S,ElemType *e) /出棧操作 if(S-top=S-base) return ERROR; *e=*-S-top; printf("%dn",e); return OK; int StackEmpty(SqStack *S) /判斷棧是否為空 if(S-top=S-base) return OK; else retur

13、n ERROR; void Input(char bMM) /輸入時候請注意把一圈都輸入為墻即'#' int i, j; printf("qingshuirumigongxingzhuang:n"); for (i = 0; i < M; i+) for (j = 0; j < M; j+) scanf("%c",&bij); getchar();/吃掉內(nèi)存中的殘留換行符號 void Ouput(const char bMM) int i, j; printf("migongxingzhuangwei:n&q

14、uot;); for (i = 0; i < M; i+) for (j = 0; j < M; j+) printf("%c",bij); printf("n"); int FindWay(char mazeMM) ElemType e; int constep = 1; int x = 1, y = 1; SqStack S; InitStack(&S); do if (mazexy = ' ') /當?shù)?次時 mazexy!=' ' 照樣通不過 mazexy = '1' e.x =

15、 x; e.y = y; e.dir = 1; Push(&S,e); if (x = M-2 && y = M-2) printf("cunzaichukoun"); return 1; NextStep(&x,&y,1); constep+; else Pop(&S,&e); while (e.dir = 4 && !StackEmpty(&S) mazee.xe.y = '0' Pop(&S,&e); if (e.dir < 4) e.dir+; Push(&S,e); x = e.x; y = e.y; NextStep(&x, &y, e.dir); else printf("meiyouchukoun"); return 0; while(S.top!=S.base); return 0; int NextStep(int *x, in

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論