老鼠走迷宫二
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:老鼠走迷宫二;
博客时间:2014-4-12;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008 32位编译器;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
引言
有些事情要不一直做,每天都做,要不就一点都别做。
题目
老鼠走迷宫二,求出所有的可以通过的路径。
思路
回溯算法,运用工具栈。
个人思路:
对于老鼠现在的位置,一共有四个方向可以走,然后在考虑有没有墙阻挡,考虑方向每次尽量一致,如下红色地方开始,也结束于此
前进
如果走过,就要留下脚印;
后退
擦去刚才踩过的脚印;继续寻找可以前进的方向(但是不要走老路)
直至走完或者退回到起点
实验
程序运行截图如下
用图画出来,如下
first sulotion follows
second solution follows
代码
test.cpp
#include<iostream> #include<stack> #include<fstream> #include<utility> using namespace std; // function:Mise mazes // input: a maze with array // output: void // 功能: Mise mazes 老鼠走迷宫 void _Mise_mazes(int ** Maze,int row,int column); // function: output path // input: a stack <int,int> // output: void // 功能: 输出结果路径 void _Output_path(stack<std::pair<int,int>> result); 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>> ; stack<int> *dir = new stack<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; int d = 0; while(! result->empty()) { // push path; now = result -> top(); if(now.first == row - 1 && now.second == column -1) { cout<<"find path"<<endl; _Output_path(*result); goto POP; } else if(now.second < column-1 && Maze[now.first][now.second+1] == 0 && d <= 0) { result -> push(std::make_pair(now.first,now.second+1)); dir->push(1); d = 0; Maze[now.first][now.second+1] = 2; } else if( now.first > 0 && Maze[now.first-1][now.second] == 0 && d <= 1) { result -> push(std::make_pair(now.first-1,now.second)); dir ->push(2); d = 0; Maze[now.first-1][now.second] = 2; } else if( now.second > 0 && Maze[now.first][now.second-1] == 0 && d <= 2) { result -> push(std::make_pair(now.first,now.second-1)); dir->push(3); d = 0; Maze[now.first][now.second-1] = 2; } else if(now.first < row - 1 && Maze[now.first+1][now.second] == 0 && d <= 3) { result -> push(std::make_pair(now.first+1,now.second)); dir->push(4); d = 0; Maze[now.first+1][now.second] = 2; } // pop stack else if(!result->empty()) { POP: now = result->top(); result->pop(); Maze[now.first][now.second] = 0; if(!dir->empty()) { d = dir->top(); dir->pop(); } } } cout<<"not else path"<<endl; } delete result ; } else { cout<<"exception of function _Mise_mazes input"<<endl; } } // function: output path // input: a stack <int,int> // output: void // 功能: 输出结果路径 void _Output_path(stack<std::pair<int,int>> result) { std::pair<int,int> top ; cout<<"solution follows"<<endl; while(! result.empty()) { top = result.top(); result.pop(); cout<<top.first<<" "<<top.second<<endl; } }
data.txt(存放的是迷宫信息)4 4 0 0 1 0 1 0 0 1 1 0 0 0 0 1 1 0