直接上代码
#include <iostream>
#include <stack>
using namespace std;
//variable define
int maze[10][10]
={ {1,1,1,0,0,0,1,1,1,1},
{1,0,1,0,1,0,0,0,0,0},
{1,0,1,0,1,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1},
{1,0,1,0,1,1,1,1,1,1},
{1,0,0,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1}
};
int delta[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool **isvisited;
class Point{
public:
int x;
int y;
};
stack<Point> path;
//function define
void find_next(Point &a,int b){
a.x += delta[b][0];
a.y += delta[b][1];
}
void initial(){
isvisited = new bool*[10];
for(int i=0;i<10;i++)
isvisited[i]=new bool[10];//default as true;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
isvisited[i][j]=true;
}
void print(){
stack<Point> outPath;
Point p;
while(!path.empty()){
outPath.push(path.top());
path.pop();
}
while(!outPath.empty()){
p = outPath.top();
cout<<" ("<<p.x<<","<<p.y<<")";
outPath.pop();
}
}
void finishTheMaze(Point &p1,int x,int y){
int i,j;
int direaction = 0;
bool flag=false;
//cout<<p1.x<<p1.y;
while(!path.empty()){
bool popflag=true;
for(direaction=0;direaction<4;direaction++){
i = p1.x;
j = p1.y;
i+=delta[direaction][0];
j+=delta[direaction][1];
if(i<0||i>9||j<0||j>9) //首先判断是否越界
continue;
if(maze[i][j]<1&&isvisited[i][j]){
find_next(p1,direaction);
path.push(p1);
isvisited[i][j]=false;
popflag=false;//如果为true则该点没有找到路径 则需要弹出返回到原来的路径中
if(i==x&&j==y){
flag = true;// if get the destination
}
break;//结束本次操作
}
}//end for
if(flag){
print();
}
if(popflag&&!path.empty()){
path.pop();//弹出
p1=path.top();//将新的点赋给p1
}
}
if(!flag) cout<<"没有路径!"<<endl;
}
int main(){
initial();
Point *p1 = new Point();
p1->x=1;
p1->y=1;
path.push(*p1);//把起点压栈操作
finishTheMaze(*p1,1,9);//传入终点
}
随机化生成矩阵迷宫,并输入相应的起点终点
起点终点默认为0 墙为1
#include <iostream>
#include <stack>
#include<stdlib.h> //rand function is included
using namespace std;
//variable define;
int **maze;
int delta[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool **isvisited;
class Point{
public:
int x;
int y;
};
stack<Point> path;
//function define
void find_next(Point &a,int b){
a.x += delta[b][0];
a.y += delta[b][1];
}
void initial(int N,int M){
maze = new int*[N];
for(int i=0;i<N;i++)
maze[i]=new int[M];
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
maze[i][j]=rand()%2;
isvisited = new bool*[N];
for(int i=0;i<N;i++)
isvisited[i]=new bool[M];//default as true;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
isvisited[i][j]=true;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++)
cout<<maze[i][j];
cout<<endl;
}
}
void print(){
stack<Point> outPath;
Point p;
while(!path.empty()){
outPath.push(path.top());
path.pop();
}
cout<<"路径如下:"<<endl;
while(!outPath.empty()){
p = outPath.top();
cout<<"("<<p.x<<","<<p.y<<") ";
outPath.pop();
}
}
void finishTheMaze(Point &p1,int x,int y,int N,int M){
int i,j;
int direaction = 0;
bool flag=false;
//cout<<p1.x<<p1.y;
while(!path.empty()){
bool popflag=true;
for(direaction=0;direaction<4;direaction++){
i = p1.x;
j = p1.y;
i+=delta[direaction][0];
j+=delta[direaction][1];
if(i<0||i>N||j<0||j>M) //首先判断是否越界
continue;
if(maze[i][j]<1&&isvisited[i][j]){
find_next(p1,direaction);
path.push(p1);
isvisited[i][j]=false;
popflag=false;//如果为true则该点没有找到路径 则需要弹出返回到原来的路径中
if(i==x&&j==y){
flag = true;
break;
}
break;//结束本次操作
}
}//end for
if(flag){
print();
}
if(popflag&&!path.empty()){
path.pop();//弹出
p1=path.top();//将新的点赋给p1
}
}
if(!flag) cout<<"没有路径!"<<endl;
}
int main(){
int N,M;
cout<<"please input the size of matrix"<<endl;
cin>>N>>M;
initial(N,M);
cout<<"please input the start"<<endl;
Point *p1 = new Point();
cin>>p1->x;
cin>>p1->y;
cout<<p1->x<<p1->y<<endl;
path.push(*p1);//把起点压栈操作
cout<<"please input the destination"<<endl;
int des_x,des_y;
cin>>des_x>>des_y;
finishTheMaze(*p1,des_x,des_y,N,M);//传入终点
}
关于迷宫问题的最短路径还需要补充