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
/************************************************************************/
/*自定义栈 */
/*通过自定义的简单栈以满足迷宫求解 */
/*功能:push() 将元素加入栈中 */
/* pop() 退栈;topValue() 获得栈顶元素 */
/* clear() 清除栈 length() 获得栈中元素个数*/
/************************************************************************/
#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;
}
//迷宫求解的方法类
//功能:通过findPath() 方法实现对路径的查找
// 通过printPath()方法将路径打印出来
#include "PathStack.h"
#include <iostream>
using namespace std;
class MazeSolveMethod
{
private:
static int maze[10][10];//存放迷宫数据
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<<"进入方法!"<<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;
}
}
/************************************************************************/
/*迷宫求解----栈方法实现*/
//功能:通过对栈实现迷宫算法求解
//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;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有