




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 新能源汽車電控工程師崗位面試問題及答案
- 2025屆重慶市主城四區(qū)高一下化學(xué)期末復(fù)習(xí)檢測(cè)試題含解析
- 廣西玉林、柳州市2025年高一下化學(xué)期末聯(lián)考模擬試題含解析
- 廣東省深圳市南山區(qū)南頭中學(xué)2025屆高二下化學(xué)期末預(yù)測(cè)試題含解析
- 江蘇省南京梅山高級(jí)中學(xué)2025年化學(xué)高二下期末檢測(cè)試題含解析
- 2025屆湖北省鄂東南五校一體聯(lián)盟聯(lián)考高二下化學(xué)期末質(zhì)量跟蹤監(jiān)視試題含解析
- 縣區(qū)培訓(xùn)材料管理辦法
- 跨境旅游品牌策略-洞察及研究
- 村級(jí)畜牧獸醫(yī)管理辦法
- 廈門采購方式管理辦法
- 中華人民共和國(guó)能源法
- 外賣配送人員勞動(dòng)合同
- 《義務(wù)教育數(shù)學(xué)課程標(biāo)準(zhǔn)(2022年版)》初中內(nèi)容解讀
- 精神疾病患者的麻醉管理
- 高一物理競(jìng)賽試題及答案
- 醫(yī)院預(yù)約平臺(tái)建設(shè)方案
- 生命體征課件教學(xué)課件
- 2024年全國(guó)環(huán)保產(chǎn)業(yè)職業(yè)技能競(jìng)賽(工業(yè)廢水處理工)考試題庫(含答案)
- 《烏魯木齊市國(guó)土空間總體規(guī)劃(2021-2035年)》
- HJ 651-2013 礦山生態(tài)環(huán)境保護(hù)與恢復(fù)治理技術(shù)規(guī)范(試行)
- SY-T 5333-2023 鉆井工程設(shè)計(jì)規(guī)范
評(píng)論
0/150
提交評(píng)論