實驗二 棧的應用迷宮求解_第1頁
實驗二 棧的應用迷宮求解_第2頁
實驗二 棧的應用迷宮求解_第3頁
實驗二 棧的應用迷宮求解_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、一、實驗目的1. 掌握棧的順序存儲結構的存儲特點與操作特點。2. 掌握棧的存儲表示與基本操作的實現(xiàn)方法。3. 熟悉棧的基本應用。4. 了解抽象數(shù)據(jù)類型的定義、表示與實現(xiàn)的含義。二、實驗內容編寫程序,進行調試,寫出調試正確的源代碼,給出測試結果。在棧存儲結構下,完成如下操作:迷宮求解(1)源代碼:1.h#define true 1#define false 0#define ok 1#define error 0#define infeasible -1#define overflow -2#define STACK_INIT_SIZE 30#define STACKINCREMENT 10ty

2、pedef int status;typedef structint i; int j;postype;typedef structint ord; postype seat; int di;selemtype;typedef structselemtype * base;selemtype *top;int stacksize;sqstack;sqstack s;typedef int mazetype55;mazetype maze;status initstack(sqstack &s);status push(sqstack &s,selemtype e); statu

3、s pop(sqstack &s,selemtype &e); status stackempty(sqstack s);status pass(postype curpos);postype nextpos (postype curpos,int di); status footprint(postype curpos); status markprint(postype curpos);#include<stdio.h>#include<stdlib.h>#include"1.h"status initstack(sqstack

4、&s)status push(sqstack &s,selemtype e)if(s.top-s.base>=s.stacksize) s.base=(selemtype s.base=(selemtype *)malloc(STACK_INIT_SIZE*sizeof(selemtype); if(!s.base) exit(overflow); s.top=s.base; s.stacksize=STACK_INIT_SIZE; return ok;*)realloc(s.base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(sel

5、emtype);status pop(sqstack &s,selemtype &e)if(s.top=s.base) return error; s.top-; e=*s.top; return ok; if(!s.base) exit(overflow); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; *s.top=e; s.top+; return ok;status stackempty(sqstack s)if(s.top=s.base) return true;else return false;sta

6、tus pass(postype curpos)if(curpos.i<1|curpos.i>10)|(curpos.j<1|curpos.j>10) return false;if(mazecurpos.icurpos.j=0) return true;else return false;postype nextpos (postype curpos,int di)status mazepath (mazetype maze,postype start,postype end) switch(di) return curpos; case 1:curpos.j+;br

7、eak; case 2:curpos.i+;break; case 3:curpos.j-;break; case 4:curpos.i-;break;initstack(s); postype curpos=start;/設當前位置為入口位置 selemtype e;int curstep=1;/探索第一步do if(pass(curpos)/當前位置可以通過,既是未曾走過的通道塊 else /當前位置不能通過 if(!stackempty(s) pop(s,e); while(e.di=4&&!stackempty(s) markprint(e.seat); footpri

8、nt(curpos);/留下足跡 /e=(curstep,curpos,1); e.ord=curstep; e.seat=curpos; e.di=1; push(s,e);/加入路徑 /if(curpos=end) if(curpos.i=end.i)&&(curpos.j=end.j) return true;/到達終點 curpos=nextpos(curpos,1);/下一位置是當前位置的東鄰 curstep+;/探索下一步 pop(s,e);/留下不能通過的標記,并退回一步 if(e.di<4) e.di+; push(s,e);/換下一個方向探索 curpo

9、s=nextpos(e.seat,e.di);/設定當前位置是新方向上的相鄰塊status footprint(postype curpos)status markprint(postype curpos)void main() int i,j;postype start; postype end; start.i=1; mazecurpos.icurpos.j=3; return ok; mazecurpos.icurpos.j=2; return ok; while(!stackempty(s); return false;start.j=1; end.i=3; end.j=3; printf("請輸入迷宮內容:n"); for(i=0;i<5;i+) for(j=0;j<5;j+) scanf("%d",&mazeij); if( mazepath(maze,start,end) for(i=0;i<5;

溫馨提示

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

評論

0/150

提交評論