欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++ 自定義棧實(shí)現(xiàn)迷宮求解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

C++ 自定義棧實(shí)現(xiàn)迷宮求解

一:迷宮求解

是一個(gè)鍛煉我們的算法求解能力的問(wèn)題,它的實(shí)現(xiàn)方法有很多;今天我們就介紹其中的用棧求解的方法。

二:什么是棧:

      大家應(yīng)該都有往袋子里裝東西的經(jīng)歷,在往袋子里裝滿(mǎn)東西之后,當(dāng)我們?nèi)ト〉臅r(shí)候,總是先從最后放進(jìn)去的東西的地方去取。也就是后進(jìn)先出(FILO)。雖然棧的單向性用起來(lái)會(huì)沒(méi)有鏈表那樣可以在任意位置對(duì)數(shù)據(jù)進(jìn)行操作,但是正因?yàn)槿绱藯R矌?lái)了很大的方便。

三:迷宮求解

現(xiàn)在我們要在下面的迷宮尋找一條可行的路徑

 1 1 1 1 1 1 1 1 1 1
 1 0 0 1 1 0 0 0 0 1
 1 0 0 0 1 0 1 0 0 1
 1 0 1 0 0 0 1 1 0 1
 1 0 1 0 0 0 1 1 1 1
 1 0 1 1 1 0 1 1 1 1
 1 0 1 1 1 0 1 1 1 1
 1 1 1 1 1 0 0 0 1 1
 1 1 1 1 1 1 1 0 0 1
 1 1 1 1 1 1 1 1 1 1

首先我們需要在程序中表示上面的迷宮,該問(wèn)題可以用數(shù)組實(shí)現(xiàn)

1:棧的定義

/************************************************************************/ 
/*自定義棧                                     */ 
/*通過(guò)自定義的簡(jiǎn)單棧以滿(mǎn)足迷宮求解        */ 
/*功能:push() 將元素加入棧中                 */ 
/*        pop() 退棧;topValue() 獲得棧頂元素    */ 
/*        clear() 清除棧 length() 獲得棧中元素個(gè)數(shù)*/ 
/************************************************************************/ 
#include <stack> 
#include <iostream> 
using namespace std; 
 
template<typename Elem> class PathStack: public stack<Elem> 
{ 
private: 
  int size; 
  int top; 
  Elem* listArray; 
public: 
  PathStack(int sz = DefaultListSize){ 
    size = sz; 
    top = 0; 
    listArray = new Elem[sz]; 
  } 
  ~PathStack(){ delete []listArray; } 
  void clear(){ top = 0; } 
  /****向棧中加入元素****/ 
  bool push(const Elem& item); 
  /***********退棧**********/ 
  Elem pop(); 
  /********獲得棧頂元素*******/ 
  Elem topValue() const; 
  int length() const { return top; } 
}; 
 
template<typename Elem> 
bool PathStack<typename Elem>::push(const Elem& item){ 
  if(top == size) return false; 
  listArray[top++] = item; 
  return true; 
} 
 
template<typename Elem> 
Elem PathStack<typename Elem>::pop(){ 
  Elem it; 
  if(top == 0) return it; 
  it = listArray[--top];  
  return it; 
} 
 
template<typename Elem> 
Elem PathStack<typename Elem>::topValue() const{ 
  Elem it; 
  if(top == 0) return it; 
  it = listArray[top - 1]; 
  return it; 
} 

  2:如何實(shí)現(xiàn)路徑的尋找

     1:設(shè)定尋找的方向,可以使用一個(gè)判斷語(yǔ)句;判斷起始位置周?chē)膫€(gè)地方有路就將該位置的坐標(biāo)加入到棧中,并將該位置標(biāo)記(將改位置值改為2,既將走過(guò)的位置標(biāo)記為2)

     2:判斷該位置周?chē)欠襁€有路,若沒(méi)有則退棧即退回到上一個(gè)位置;并將該位置做下另一個(gè)標(biāo)記(將該位置值改為3,既將退棧位置值用3標(biāo)記)

     3:重復(fù)1,2步驟直到達(dá)到出口

     路徑尋找的類(lèi): 

//迷宮求解的方法類(lèi) 
//功能:通過(guò)findPath() 方法實(shí)現(xiàn)對(duì)路徑的查找 
//       通過(guò)printPath()方法將路徑打印出來(lái) 
#include "PathStack.h" 
#include <iostream> 
using namespace std; 
 
class MazeSolveMethod 
{ 
private: 
  static int maze[10][10];//存放迷宮數(shù)據(jù) 
  PathStack<int> pathStack;//定義棧 
public: 
  MazeSolveMethod():pathStack(100){ 
  } 
  ~MazeSolveMethod(){ } 
  void findPath(int startX,int startY); 
  void printPath() const; 
}; 
 
int MazeSolveMethod::maze[10][10] = { 
  {1,1,1,1,1,1,1,1,1,1}, 
  {1,0,0,1,1,0,0,0,0,1}, 
  {1,0,0,0,1,0,1,0,0,1}, 
  {1,0,1,0,0,0,1,1,0,1}, 
  {1,0,1,0,0,0,1,1,1,1}, 
  {1,0,1,1,1,0,1,1,1,1}, 
  {1,0,1,1,1,0,1,1,1,1}, 
  {1,1,1,1,1,0,0,0,1,1}, 
  {1,1,1,1,1,1,1,0,0,1}, 
  {1,1,1,1,1,1,1,1,1,1}, 
}; 
 
void MazeSolveMethod::findPath(int startX,int startY){ 
  int x = startX; 
  int y = startY; 
  pathStack.push(x); 
  pathStack.push(y); 
  maze[x][y] = 2; 
  cout<<"進(jìn)入方法!"<<endl; 
  while(true){ 
    if(maze[x-1][y] == 0){ 
      pathStack.push(--x); 
      pathStack.push(y); 
      maze[x][y] = 2; 
    }else if(maze[x][y-1] == 0){ 
      pathStack.push(x); 
      pathStack.push(--y); 
      maze[x][y] = 2; 
    }else if(maze[x][y+1] == 0){ 
      pathStack.push(x); 
      pathStack.push(++y); 
      maze[x][y] = 2; 
    }else if(maze[x+1][y] == 0){ 
      pathStack.push(++x); 
      pathStack.push(y); 
      maze[x][y] = 2; 
    } 
    if(maze[x-1][y] != 0 && maze[x][y+1] != 0 && maze[x+1][y] != 0 && maze[x][y-1] != 0){         
      if(x >= 8 && y >= 8){ 
        break; 
      }else{ 
        maze[x][y] = 3; 
        y = pathStack.pop(); 
        x = pathStack.pop(); 
      } 
      y = pathStack.topValue(); 
      int temp = pathStack.pop(); 
      x = pathStack.topValue(); 
      pathStack.push(temp); 
    } 
  } 
} 
 
void MazeSolveMethod::printPath() const{ 
  cout<<"printPath"<<endl; 
  for(int i=0; i<10; i++){ 
    for(int j=0; j<10; j++){ 
      if(maze[i][j] == 2) 
        cout<<'*'<<" "; 
      else if(maze[i][j] == 3) 
        cout<<0<<" "; 
      else 
        cout<<1<<" "; 
    } 
    cout<<endl; 
  } 
} 

   主函數(shù)類(lèi) 

/************************************************************************/ 
/*迷宮求解----棧方法實(shí)現(xiàn)*/ 
//功能:通過(guò)對(duì)棧實(shí)現(xiàn)迷宮算法求解 
//Author:Andrew 
//Date  :2012-10-20 
/************************************************************************/ 
#include "MazeSolveMethod.h" 
#include <iostream> 
using std::cout; 
using std::endl; 
 
int main(){ 
 
  MazeSolveMethod solve; 
  solve.findPath(1,1); 
  solve.printPath(); 
  system("pause"); 
  return 0; 
} 

 將上面的代碼運(yùn)行后結(jié)果截圖如下:

 其中* 為路徑 

 

 感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

上一篇:C++ 類(lèi)的繼承與派生實(shí)例詳解

欄    目:C語(yǔ)言

下一篇:C++中的菱形繼承深入分析

本文標(biāo)題:C++ 自定義棧實(shí)現(xiàn)迷宮求解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1399.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有