




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、/C+實(shí)現(xiàn)動(dòng)態(tài)迷宮求解,基于類和棧結(jié)構(gòu);/迷宮大小、構(gòu)造以及入口出口位置可人為設(shè)定;/動(dòng)態(tài)顯示尋找出口的過程,最終顯示路線。#include<iostream>#include<cstdlib>#include <windows.h>using namespace std;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define N 12 /迷宮規(guī)模/int mazeNN=/3,3,3,3,3,3,3,3,3,3,/3,0,0,3,0,0,0,3,0,3,/3,0,0,3,0,0,0,3,0,3,
2、/3,0,0,0,0,3,3,0,0,3,/3,0,3,3,3,0,0,0,0,3,/3,0,0,0,3,0,0,0,0,3,/3,0,3,0,0,0,3,0,0,3,/3,0,3,3,3,0,3,3,0,3,/3,3,0,0,0,0,3,0,0,3,/3,3,3,3,3,3,3,3,3,3/;int mazeNN=3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,3,0,0,0,3,0,0,0,3,3,0,0,3,0,0,0,3,0,3,0,3,3,0,0,0,0,3,3,0,0,3,0,3,3,0,3,3,3,0,0,3,3,0,0,3,3,0,0,0,3,0,0,0,0,0,0
3、,3,3,0,3,0,0,0,3,0,3,3,3,3,3,0,3,3,3,0,3,0,0,0,0,3,3,3,0,0,0,0,3,0,3,0,0,3,3,3,0,3,3,0,3,3,0,3,0,3,3,3,0,0,0,0,3,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,;HANDLE hOut;COORD pos;class Pospublic:Pos(int x,int y)xpos=x;ypos=y;Pos()xpos=0;ypos=0;int xpos;int ypos;bool operator = (Pos pos1,Pos pos2)if(pos1.xpos
4、=pos2.xpos&&pos1.ypos=pos2.ypos) return true;else return false;Pos start(1,1),end(9,8); /入口及出口位置class Blockpublic:Block()ord=0;di=1;int ord;Pos seat;int di;class Stackpublic:Stack();Stack()delete base;void push(Block);void pop(Block&);friend int Mazepath(Pos,Pos);friend void printsteps(S
5、tack);friend void printonestep(Stack);private:Block *top;Block *base;int stacksize;Stack:Stack()base=new BlockSTACK_INIT_SIZE;top=base;stacksize=STACK_INIT_SIZE;void Stack:push(Block e)if(top-base>=stacksize)Block *newbase=new Blockstacksize+STACKINCREMENT;for(int i=0;i<stacksize;i+)newbasei=b
6、asei;delete base;base=newbase;top=base+stacksize;stacksize+=STACKINCREMENT;*top=e;top+;void Stack:pop(Block &e)if(top=base) return;e=*-top;int pass(Pos cur)if(mazecur.xposcur.ypos=0) return 1;else return 0;void footprint(Pos cur)mazecur.xposcur.ypos=1;Pos nextpos(Pos cur,int d)Pos pos1;switch(d)
7、case 1:pos1.xpos=cur.xpos;pos1.ypos=cur.ypos+1;return pos1;case 2:pos1.xpos=cur.xpos+1;pos1.ypos=cur.ypos;return pos1;case 3:pos1.xpos=cur.xpos;pos1.ypos=cur.ypos-1;return pos1;case 4:pos1.xpos=cur.xpos-1;pos1.ypos=cur.ypos;return pos1;default:break;void markprint(Pos cur)mazecur.xposcur.ypos=2;void
8、 printsteps(Stack s)Block *p=s.base;while(p+1)!=s.top)if(p->seat.xpos=(p+1)->seat.xpos&&p->seat.ypos=(p+1)->seat.ypos-1) mazep->seat.xposp->seat.ypos=4;else if(p->seat.xpos=(p+1)->seat.xpos-1)&&p->seat.ypos=(p+1)->seat.ypos) mazep->seat.xposp->seat
9、.ypos=5;else if(p->seat.xpos=(p+1)->seat.xpos&&p->seat.ypos=(p+1)->seat.ypos+1) mazep->seat.xposp->seat.ypos=6;else if(p->seat.xpos=(p+1)->seat.xpos+1)&&p->seat.ypos=(p+1)->seat.ypos) mazep->seat.xposp->seat.ypos=7;p+;for(int i=0;i<N;i+)for(int
10、j=0;j<N;j+)if(mazeij=3) cout<<(char)91<<(char)93;else if(mazeij=4) cout<<(char)26<<' 'else if(mazeij=5) cout<<(char)25<<' 'else if(mazeij=6) cout<<(char)27<<' 'else if(mazeij=7) cout<<(char)24<<' 'else if(
11、i=end.xpos&&j=end.ypos) cout<<"$"else cout<<" "cout<<endl;cout<<endl;p=s.base;int ent=0;while(p+1)!=s.top)cout<<'('<<p->seat.xpos<<','<<p->seat.ypos<<") -> "p+;ent+;if(ent=4)ent=0;cou
12、t<<endl;cout<<'('<<p->seat.xpos<<','<<p->seat.ypos<<')'<<endl;void printonestep(Stack s)pos= (s.top-1)->seat.ypos*2,(s.top-1)->seat.xpos; /* 光標(biāo)的起始位 SetConsoleCursorPosition(hOut, pos); cout<<(char)17<<(char)16;
13、_sleep(200);pos= (s.top-1)->seat.ypos*2,(s.top-1)->seat.xpos;if(!(s.top-1)->seat.xpos=end.xpos&&(s.top-1)->seat.ypos=end.ypos) SetConsoleCursorPosition(hOut, pos); cout<<" "int Mazepath(Pos start,Pos end)Stack s;Pos curpos=start;Block e;int curstep=1;int l=1;doif(
14、pass(curpos)footprint(curpos);e.ord=curstep;e.seat=curpos;e.di=1;s.push(e);printonestep(s);if(curpos=end) pos= 0,N; SetConsoleCursorPosition(hOut, pos);cout<<endl<<"Find the path!"<<endl<<endl; printsteps(s);return 1;curpos=nextpos(curpos,1);curstep+;if(l=1)s.base-&
15、gt;ord=1;s.base->seat.xpos=start.xpos;s.base->seat.ypos=start.ypos;s.base->di=1;l=0;elseif(s.top!=s.base)s.pop(e);while(e.di=4&&s.top!=s.base)markprint(e.seat);printonestep(s);s.pop(e);if(e.di<4)e.di+;s.push(e);curpos=nextpos(e.seat,e.di);while(s.top!=s.base);return 0;int main()hOut = GetStdHandle(STD_OUTPUT_HANDLE); for(int i=0;i<N;i+)for(int j=0;j<N;j+)if(mazeij=3) cout<<(char)91<<(char)93;else if(i=end.xpos&&j=end.ypos) cout<<"$&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 在線教育平臺內(nèi)容制作手冊
- 房屋買賣合同居間協(xié)議
- 工程管理質(zhì)量與安全控制手冊
- 家具廠廠長聘任書合同
- 地皮交易居間協(xié)議合同
- 2025年綿陽貨運(yùn)從業(yè)資格證考試題庫
- 《數(shù)據(jù)可視化技術(shù)應(yīng)用》3.3 構(gòu)建銷售數(shù)據(jù)動(dòng)態(tài)分析看板-教案
- 員工上下班安全協(xié)議書5篇
- 廠房消防勞務(wù)承包合同范例
- 淮北房產(chǎn)合同范本
- 2020年中國高爾夫球運(yùn)動(dòng)行業(yè)發(fā)展現(xiàn)狀及趨勢分析:消費(fèi)大眾化、年輕化圖
- 主神空間兌換
- 《中外美術(shù)史》課件13外國美術(shù)史+中世紀(jì)美術(shù)
- 水電站生產(chǎn)準(zhǔn)備工作方案
- 《請給我結(jié)果》讀書心得-PPT課件
- HD7簡明實(shí)用操作手冊
- S水電站引水建筑物設(shè)計(jì)
- 110kV軟母線及引連線施工方案
- 鼓譜——海闊天空
- CT報(bào)告單模板
- 足球比賽計(jì)分表(共6頁)
評論
0/150
提交評論