国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

迷宮求解C/C++

2019-11-06 07:35:42
字體:
供稿:網(wǎng)友

迷宮求解

利用數(shù)據(jù)結(jié)構(gòu)中的棧來求解迷宮路徑,方法很簡(jiǎn)單,課本上也有很詳細(xì)的問題解法,直接放代碼:PS(代碼可以直接運(yùn)行,參數(shù)都放好了,要改可以在main()函數(shù)中修改,下次再放一個(gè)用BFS求迷宮的最優(yōu)路徑以及求解全部路徑的算法。)#include <iostream>#include <malloc.h>using namespace std;#define STACK_INIT_SIZE 10#define STACK_INCREMENT 2#define MAX 10typedef struct{ int x; int y;}MazePos;typedef struct{ MazePos *top; MazePos *base; int StackSize;}SqStack;void InitStack(SqStack &S){ S.base = (MazePos *) malloc (STACK_INIT_SIZE * sizeof(MazePos)); S.top = S.base; S.StackSize = STACK_INIT_SIZE;}bool StackEmpty(SqStack S){ if(S.top == S.base) return true; else return false;}void Push(SqStack &S, MazePos e){ if((S.top - S.base) >= S.StackSize){ S.base = (MazePos *) realloc (S.base, (S.StackSize + STACK_INCREMENT) * sizeof(MazePos)); S.top = S.base + S.StackSize; S.StackSize += STACK_INCREMENT; } *S.top = e; ++S.top;}void Pop(SqStack &S, MazePos &e){ if(!StackEmpty(S)) e = *--S.top;}void GetTop(SqStack S, MazePos &e){ if(S.top > S.base) e = *--S.top;}void visit(MazePos e){ cout<<"("<<e.x<<","<<e.y<<")";}void StackTraverse(SqStack S){ int i = 0; cout<<"start"; while(S.base < S.top){ cout<<"->"; visit(*S.base++); if(++i%8 == 0) cout<<endl; } cout<<"->end"<<endl;}void MazePRint(int Maze[][MAX], int n,int m){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++) printf("%3d",Maze[i][j]); cout<<endl; }}void MazePath(int Maze[][MAX], MazePos start, MazePos end){ int flag,count = 1; MazePos curpos; SqStack s; InitStack(s); curpos = start; if(!Maze[start.x][start.y] || !Maze[end.x][end.y]) goto loop; do{ if(Maze[curpos.x + 1][curpos.y] == 1){ Push(s,curpos); ++curpos.x; flag = 1; } else if(Maze[curpos.x][curpos.y + 1] == 1){ Push(s,curpos); ++curpos.y; flag = 1; } else if(Maze[curpos.x - 1][curpos.y] == 1){ Push(s,curpos); --curpos.x; flag = 1; } else if(Maze[curpos.x][curpos.y - 1] == 1){ Push(s,curpos); --curpos.y; flag = 1; } else flag = 0; if(flag) Maze[curpos.x][curpos.y] = ++count; else{ Maze[curpos.x][curpos.y] = 0; Pop(s,curpos); --count; } }while((curpos.x != end.x || curpos.y != end.y) && !StackEmpty(s)); if(curpos.x == end.x && curpos.y == end.y){ cout<<"Yes!"<<endl; MazePrint(Maze, MAX, MAX); StackTraverse(s); } else loop:cout<<"No!"<<endl;}int main(){ int Maze[MAX][MAX]={ {0,0,0,0,0,0,0,0,0,0}, {0,1,0,1,0,1,1,1,1,0}, {0,1,1,1,0,1,0,1,0,0}, {0,0,1,0,0,0,1,1,1,0}, {0,1,1,1,1,1,1,0,1,0}, {0,0,0,0,0,0,0,0,1,0}, {0,1,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}}; MazePos start, end; start.x = start.y = 1; end.x = end.y=8; cout<<"Print Maze"<<endl; MazePrint(Maze, MAX, MAX); MazePath(Maze, start, end); return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 海南省| 齐齐哈尔市| 大埔县| 漳州市| 通河县| 轮台县| 田阳县| 梧州市| 白水县| 桐城市| 长子县| 樟树市| 永春县| 麻栗坡县| 兴安盟| 马公市| 西乡县| 保山市| 汕头市| 武夷山市| 安溪县| 武山县| 英德市| 五台县| 莲花县| 海盐县| 湘乡市| 吴旗县| 梧州市| 社旗县| 天峻县| 南郑县| 阳江市| 平陆县| 西盟| 大荔县| 东阿县| 集贤县| 长海县| 韩城市| 利川市|