一、實(shí)驗(yàn)?zāi)康?/h2>
理解棧的抽象數(shù)據(jù)類(lèi)型定義及操作特點(diǎn)。掌握順序棧的存儲(chǔ)結(jié)構(gòu)的描述。掌握順序棧的基本操作的實(shí)現(xiàn)方法。理解棧的廣泛應(yīng)用。
二、預(yù)備知識(shí)
閱讀課程教材P44~45頁(yè)內(nèi)容,掌握棧的邏輯定義及“后進(jìn)先出”的特點(diǎn),理解抽象數(shù)據(jù)類(lèi)型棧的定義。閱讀課程教材P45~47頁(yè)內(nèi)容,理解順序棧的存儲(chǔ)特點(diǎn)及存儲(chǔ)表示,掌握順序棧各種基本操作(InitStack、StackEmpty、GetTop、Push、Pop等)的實(shí)現(xiàn)方法。閱讀課程教材P50~52頁(yè)內(nèi)容,理解“迷宮求解”問(wèn)題的含義,體會(huì)求解過(guò)程中棧的應(yīng)用。仔細(xì)分析主要實(shí)現(xiàn)算法,理解求解步驟和方法。
三、實(shí)驗(yàn)內(nèi)容
按如下要求編寫(xiě)程序,進(jìn)行調(diào)試,寫(xiě)出調(diào)試正確的源代碼,給出測(cè)試結(jié)果。
1.完成順序棧的存儲(chǔ)表示,實(shí)現(xiàn)順序棧的各種基本操作,包括InitStack、StackEmpty、GetTop、Push、Pop等操作。
2.利用順序棧求解迷宮中從入口到出口的一條路徑,并輸出結(jié)果。
說(shuō)明:
(1)使用二維數(shù)組maze描述迷宮,迷宮的規(guī)模及初態(tài)自定。
(2)路徑的輸出形式可用文字描述,也可用圖形描述。
定義一些代碼:
#include<iostream> #include<cstdlib> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef struct {//棧元素類(lèi)型 int x;//坐標(biāo) int y;//坐標(biāo) int di;//方向 }position; using namespace std; typedef struct {//棧 position *base; position *top; int stacksize; }Stack; /*************************迷宮**********************************/ int Maze[10][10] = {//迷宮 Maze(妹子)原型如下圖:1表示路不通0表示可以通過(guò)。 // 0 1 2 3 4 5 6 7 8 9 {1,1,1,1,1,1,1,1,1,1},//0 {1,0,0,1,0,0,0,1,0,1},//1 {1,0,0,1,0,0,0,1,0,1},//2 {1,0,0,0,0,1,1,0,0,1},//3 {1,0,1,1,1,0,0,0,0,1},//4 {1,0,0,0,1,0,0,0,0,1},//5 {1,0,1,0,0,0,1,0,0,1},//6 {1,0,1,1,1,0,1,1,0,1},//7 {1,1,0,0,0,0,0,0,0,1},//8 {1,1,1,1,1,1,1,1,1,1} //9 };
定義類(lèi)
class boos {//創(chuàng)建了一個(gè)角色類(lèi) private: Stack sq_stack;//棧 position temp; public: /******************************棧的基本方法*******************/ void InitStack() {//創(chuàng)建棧 bool StackEmpty()//判斷是否空棧 bool GetTop(position &temp)//獲得棧頂 bool Push(position &temp)//入 bool Pop(position &temp)//出棧 void free_Stack()//釋放棧空間 /******************************走迷宮方法*******************/ bool findMaze(int star_x, int star_y, int endr_x, int end_y) //迷宮的入口和出口坐標(biāo) };
類(lèi)的成員函數(shù)的一些說(shuō)明:
這是一些基礎(chǔ)方法 用于對(duì)棧的操作。
void InitStack() {//創(chuàng)建空的棧 sq_stack.base = (position *)malloc(sizeof(Stack)*STACK_INIT_SIZE); if (!sq_stack.base) exit(-1); sq_stack.top = sq_stack.base;/*FHL*/ sq_stack.stacksize = STACK_INIT_SIZE; cout << "棧創(chuàng)建成功" << endl; } bool StackEmpty() {判斷是否空棧 if (sq_stack.top == sq_stack.base)return 1; else return 0; } bool GetTop(position &temp) {//得到棧頂元素 if (StackEmpty())return false; temp= *(sq_stack.top-1); return true; } bool Push(position &temp){//入棧/*FHL*/ if (sq_stack.top - sq_stack.base >= sq_stack.stacksize) { sq_stack.base = (position*)realloc(sq_stack.base sizeof(position)*(sq_stack.stacksize + STACKINCREMENT)); if(!sq_stack.base) exit(-1);/*FHL*/ sq_stack.top = sq_stack.base + sq_stack.stacksize; sq_stack.stacksize += STACKINCREMENT; } *sq_stack.top = temp; sq_stack.top++; return true; } bool Pop(position &temp) {//出棧 if (StackEmpty()) return 0; sq_stack.top--; temp = *sq_stack.top; return 1; } void free_Stack() { free(sq_stack.base); }
找迷宮的方法(dfs算法)
bool findMaze(int star_x, int star_y, int endr_x, int end_y) {//迷宮的入口和出口坐標(biāo) int i, j, k = 0;//i j表示目前的坐標(biāo) int tep_di,next_x,tep_y;//下一步的坐標(biāo) bool flag; position fan_maze[200]; InitStack();//先創(chuàng)建空棧 temp.x = star_x, temp.y = star_y, temp.di - 1;//開(kāi)始位置 Push(temp);//入棧操作。/*FHL*/ Maze[star_x][star_y]=-1;//-1表示走過(guò); while (!StackEmpty()) {//棧不為空 GetTop(temp);/*FHL*/ i = temp.x, j = temp.y , tep_di=temp.di; if (i == endr_x && j == end_y) { cout << "找到走出迷宮的路" << endl; k = 0; while (!StackEmpty()) { Pop(temp); fan_maze[k] = temp; k++;//k指向下一個(gè)被插入的位置; } cout <<"起點(diǎn):"<< "(" << fan_maze[k-1].x << "," << fan_maze[k-1].y << ")->" << endl; int count = 1; for(k-=2;k>0;k--) { cout<<"(" << fan_maze[k].x <<","<< fan_maze[k].y<<")->"; if (count % 3 == 0) cout << endl; count++; } cout << "(" << fan_maze[0].x << "," << fan_maze[0].y << ")" << "終點(diǎn)" << endl;//出口的位置 free_Stack();//釋放申請(qǐng)的堆空間 return true; }/*FHL*/ flag = 0; while (tep_di < 4 && !flag) { tep_di++; if (tep_di == 0){ next_x = i; tep_y = j + 1;} else if (tep_di == 1) { next_x = i + 1;tep_y = j; } else if (tep_di == 2) { next_x = i;tep_y = j - 1; } else { next_x = i - 1; tep_y = j; } if( Maze[next_x][tep_y] == 0 ) flag = 1; } if(flag) { (sq_stack.top-1)->di = tep_di;//記錄上次坐標(biāo)走的方向。 temp.x = next_x, temp.y = tep_y,temp.di=-1; Push(temp);//這次坐標(biāo)入棧 Maze[next_x][tep_y] = -1;//當(dāng)前坐標(biāo)標(biāo)記為走過(guò)。 } else { Pop(temp); Maze[temp.x][temp.y] = 0; } }/*FHL*/ cout << "沒(méi)有找到對(duì)應(yīng)的出口" << endl; free_Stack();//釋放申請(qǐng)的堆空間 return false; } };
主函數(shù)(創(chuàng)建對(duì)象)
int main() { boos L1; L1.findMaze(1,1,8,8); system("pause");/*FHL*/ return 0; }
運(yùn)行的一些截圖:
1.當(dāng)入口和終點(diǎn)一樣時(shí):
int main() { boos L1; L1.findMaze(1,1,1,1); system("pause"); return 0; }
2.終點(diǎn)是可以到達(dá)的路徑
2.1(8,8)是終點(diǎn)
int main() { boos L1; L1.findMaze(1,1,8,8); system("pause"); return 0; }
2.2(8,2)是終點(diǎn)
int main() { boos L1; L1.findMaze(1,1,8,2); system("pause"); return 0; }
3.出口不通的情況
int main() { boos L1; L1.findMaze(1,1,9,9); system("pause"); return 0; }
以上就是C++數(shù)據(jù)結(jié)構(gòu)關(guān)于棧迷宮求解示例的詳細(xì)內(nèi)容,更多關(guān)于C++數(shù)據(jù)結(jié)構(gòu)棧迷宮的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/weixin_52062043/article/details/121062726