个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献
博客内容:老鼠走迷宫
博客时间:2014-4-9
编程语言:C++
编程坏境:Windows 7 专业版 x64
编程工具:vs2008 32位编译器
制图工具:office 2010 ppt
引言
有些事情要不一直做,每天都做,要不就一点都别做。
题目
老鼠走迷宫,stack 的典型教案。
思路
利用表格作为迷宫,0 代表可以过,1代表不可以过
老鼠走过的脚印用 2 标记
举个例子,迷宫如下
老鼠可以可以到达终点的路径如下红色线标记
老鼠从开始节点走,每进步一格,就标记格子已经走过,然后把脚印放入栈里面
伪代码思路如下
maze 是代表迷宫的二维数组
result是一个栈,stack<pair<int,int>>
result->push(std::make_pair(0,0)); Maze[0][0] = 2; while(! result->empty()) { // push path; now = result -> top(); if(now.first == row - 1 && now.second == column -1) { cout<<"find path"<<endl; break; } else if(now.second < column-1 && Maze[now.first][now.second+1] == 0 ) { result -> push(std::make_pair(now.first,now.second+1)); Maze[now.first][now.second+1] = 2; } else if( now.first > 0 && Maze[now.first-1][now.second] == 0) { result -> push(std::make_pair(now.first-1,now.second)); Maze[now.first-1][now.second] = 2; } else if( now.second > 0 && Maze[now.first][now.second-1] == 0) { result -> push(std::make_pair(now.first,now.second-1)); Maze[now.first][now.second-1] = 2; } else if(now.first < row - 1 && Maze[now.first+1][now.second] == 0) { result -> push(std::make_pair(now.first+1,now.second)); Maze[now.first+1][now.second] = 2; } // pop stack else if(!result->empty()) { result->pop(); } } if(!result->empty()) { std::pair<int,int> path; while(!result->empty()) { path = result->top(); cout<<path.first<<" "<<path.second<<endl; result->pop(); } } else cout<<"not find path"<<endl;
实验
一个封闭迷宫例子
程序运行结果
一个不封闭迷宫例子
程序运行结果
程序解释
find path 之后
每一行是 老鼠可以穿过迷宫的路线,即二维数组的行和列的下标
代码
test.cpp
#include<iostream> #include<stack> #include<fstream> #include<utility> using namespace std; // function: Mise mazes void _Mise_mazes(int **Maze,int row,int column); int main() { int row ,column ; ifstream reader ; reader.open("data.txt"); reader>>row>>column; int **Maze = new int*[row]; for(int i=0 ; i<row; i++) { Maze[i] = new int[column]; } for(int i=0; i<row; i++) for(int j=0; j<column; j++) { reader>>Maze[i][j]; } for(int i= 0;i<row;i++) { for(int j=0;j<column;j++) { cout<<Maze[i][j]<<" "; } cout<<endl; } reader.close(); _Mise_mazes(Maze,row,column); system("pause") ; return 0 ; } // function:Mise mazes // input: a maze with array // output: void // 功能: Mise mazes 老鼠走迷宫 void _Mise_mazes(int ** Maze,int row,int column) { if(Maze != NULL && row > 0 && column > 0) { // first action: make space stack<std::pair<int,int>> * result = new stack<std::pair<int,int>> ; if(Maze[0][0] != 0) { cout<<"no path for Mise"<<endl; } else { int i,j; std::pair<int,int> now; result->push(std::make_pair(0,0)); Maze[0][0] = 2; while(! result->empty()) { // push path; now = result -> top(); if(now.first == row - 1 && now.second == column -1) { cout<<"find path"<<endl; break; } else if(now.second < column-1 && Maze[now.first][now.second+1] == 0 ) { result -> push(std::make_pair(now.first,now.second+1)); Maze[now.first][now.second+1] = 2; } else if( now.first > 0 && Maze[now.first-1][now.second] == 0) { result -> push(std::make_pair(now.first-1,now.second)); Maze[now.first-1][now.second] = 2; } else if( now.second > 0 && Maze[now.first][now.second-1] == 0) { result -> push(std::make_pair(now.first,now.second-1)); Maze[now.first][now.second-1] = 2; } else if(now.first < row - 1 && Maze[now.first+1][now.second] == 0) { result -> push(std::make_pair(now.first+1,now.second)); Maze[now.first+1][now.second] = 2; } // pop stack else if(!result->empty()) { result->pop(); } } if(!result->empty()) { std::pair<int,int> path; while(!result->empty()) { path = result->top(); cout<<path.first<<" "<<path.second<<endl; result->pop(); } } else cout<<"not find path"<<endl; } delete result ; } else { cout<<"exception of function _Mise_mazes input"<<endl; } }
原文:http://blog.csdn.net/cqs_experiment/article/details/23363101