[C++]資料結構課程設計:迷宮老鼠1.0

來源:互聯網
上載者:User
/*************************************************************************    著作權:汪海的實驗室      作者:汪海洋         日期:2013-03-11    描述:資料結構課程設計,使用堆棧解決迷宮的尋徑問題         **************************************************************************/#include <Windows.h>#include<iostream>#include<stack>using namespace std;//Position定義座標struct Position{int row;int col;};  //Maze類建立迷宮並尋找最短路徑class Maze{private:int row;//迷宮的寬度int col;//迷宮的高度int show;//1=示範尋徑流程 或者 0=不示範尋徑流程Position offset[4];//上下左右的位移量stack<Position> path;//儲存路徑的堆棧char** maze;//迷宮地圖的數組char** mazeShow;//用來顯示迷宮尋徑結果的數組public:Maze(int row,int col,int show);void ShowArray(char**array,int m,int n);//輸出m*n的二維數組bool FindPath();//尋找迷宮出口的方法void DrawPath();//繪製迷宮的尋徑結果void ShowDetail(Position here);//展示尋徑的流程};//迷宮資料的輸入和位移量的初始化Maze::Maze(int row,int col,int show){this->row = row;this->col = col;this->show = show;//初始化maze二維數組maze = new char*[row+2];for(int i=0; i<row+2; ++i)maze[i] = new char[col+2];//初始化mazeShow二維數組mazeShow = new char*[row+2];for(i=0; i<row+2; ++i)mazeShow[i] = new char[col+2];//對位移量進行初始化offset[0].row = 0;offset[0].col = 1;//向右offset[1].row = 1;offset[1].col = 0;//向下offset[2].row = 0;offset[2].col = -1;//向左offset[3].row = -1;offset[3].col = 0;//向上//輸入迷宮的地圖資料int mazeData[10][10] = {{0,1,1,1,1,1,0,0,0,0},{0,0,0,0,0,1,0,1,0,0},{0,0,0,1,0,1,0,0,0,0},{0,1,0,1,0,1,0,1,1,0},{0,1,0,1,0,1,0,1,0,0},{0,1,1,1,0,1,0,1,0,1},{0,0,0,0,0,0,0,1,0,1},{0,0,0,1,1,0,0,1,0,0},{1,0,0,0,0,0,0,1,0,0},{0,0,0,0,1,0,0,1,0,0},};//將迷宮資料匯入迷宮數組中for (int ii = 0;ii<row;ii++){for (int jj = 0;jj<col;jj++){maze[ii+1][jj+1]=mazeData[ii][jj];}}//在迷宮外增加一圈障礙物for (int r = 0;r<=row+1;r++){maze[0][r] = maze[row+1][r] = 1;}for (int c = 0;c<=col+1;c++){maze[c][0] = maze[c][col+1] = 1;}for (int iii = 0;iii<row+2;iii++){for (int jjj = 0;jjj<col+2;jjj++){mazeShow[iii][jjj] = maze[iii][jjj];}}if(show){for (int t = 0; t<30;t++)cout<<endl;}cout<<endl<<"原始的迷宮地圖:"<<endl;ShowArray(maze,row+2,col+2);}//遍曆輸出二維數組void Maze::ShowArray(char**array,int m,int n){for (int i = 0;i<m;i++){for (int j = 0;j<n;j++){cout<<array[i][j]<<" ";}cout<<endl;}}//顯示具體的尋徑流程void Maze::ShowDetail(Position here){Sleep(500);for (int t = 0; t<30;t++)cout<<endl;char me = 2;for (int i = 0;i < row+2;i++){for (int j = 0;j<col+2;j++){if (i==here.row&&j==here.col){cout<<me<<" ";} else{cout<<maze[i][j]<<" ";}}cout<<endl;}}//尋找從位置(1,1)到出口(m,m)的路徑//如果成功則true,否則返回falsebool Maze::FindPath(){Position here;here.row = 1;here.col = 1;maze[1][1] = 1;int option = 0;//選擇的移動方向int LastOption = 3;//最後一個方向選擇//尋找一條路徑while (here.row!=row||here.col!=col){//尋找並且移動到一個相鄰的位置int r,c;while(option<=LastOption){r = here.row + offset[option].row;c = here.col + offset[option].col;if(maze[r][c] == 0)break;option++;}//是否找到一個相鄰的位置if (option<=LastOption){//移動到maze[r][c]//將當前位置壓入堆棧path.push(here);here.row = r;here.col = c;//設定障礙物以阻止再次訪問maze[r][c] = 4;option = 0;} else{//沒有可用的相鄰位置,回溯上一個節點if (path.empty())return false;Position next = path.top();path.pop();option = 0;here = next;}if (show){ShowDetail(here);}}return true;}//繪製最終的尋徑結果void Maze::DrawPath(){Position temp;while(!path.empty()){temp = path.top();path.pop();mazeShow[temp.row][temp.col] = '+';}mazeShow[row][col] = 3;mazeShow[1][1] = 2;if(show){for (int t = 0; t<30;t++)cout<<endl;}cout<<endl<<"尋徑後的迷宮地圖:"<<endl;ShowArray(mazeShow,row+2,col+2);}void main(){cout<<"歡迎來到迷宮老鼠1.0版本!\n是否需要顯示迷宮尋徑的具體流程?(Y/N)"<<endl;char input;cin>>input;int show ; //是否展示迷宮尋徑流程,1展示,0不展示if (input=='Y'||input=='y')show = 1; else if (input=='N'||input=='n')show = 0;else return ;Maze* myMaze = new Maze(10,10,show);myMaze->FindPath();myMaze->DrawPath();}

項目:http://download.csdn.net/detail/wxg694175346/5135742

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.