c++stack算法求问
#include "StdAfx.h"
#include <iostream>
#include <stack>
using namespace std;
#define MaxSize 100
#define M 10
#define N 10
int MAZE[M][N]=
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
struct pos
{
int i,j;
};
struct pos Move(struct pos curr)
{
/*判断是否有通路,有则返回通路位置,无则返回原来位置
重复问题:
若何判断原来方向
若何记录哪个位置是第几次走*/
struct pos next=curr;
int x=curr.i,y=curr.j;
//向上
if(!MAZE[x-1][y]&&(x-1)>=0 && MAZE[x-1][y]!=2)
{
next.i=x-1;//只有x坐标改变
MAZE[x-1][y]=2;//标记将要行走
return next;//返回要下一步的位置
}//向右
else if(!MAZE[x][y+1]&&(y+1)<N && MAZE[x][y+1]!=2)
{
next.j=y+1;
MAZE[x][y+1]=2;
return next;
}//向下
else if(!MAZE[x+1][y]&&(x+1)<M && MAZE[x+1][y]!=2)
{
next.i=x+1;
MAZE[x+1][y]=2;
return next;
}//向左
else if(!MAZE[x][y-1]&&y-1>=0 && MAZE[x][y-1]!=2)
{
next.j=y-1;
MAZE[x][y-1]=2;
return next;
}
return curr;//无路,返回原来的位置
}
void findPath()
{
stack<struct pos> Path;
struct pos curr,nex;
curr.i=0;curr.j=0;
Path.push(curr);//入口入栈
MAZE[0][0]=2;//标记已走过
while(!Path.empty()){//5)
curr=Path.top();//1)
// cout<<curr.i<<" "<<curr.j<<endl;
nex=curr;
nex=Move(curr);//2)特别注意重复的问题和边界处理问题
if(!(curr.i==nex.i&&curr.j==nex.j))//3)发现通路
Path.push(nex);
else
Path.pop();//4)未发现通路
if(nex.i==M-1&&nex.j==N-1){//5)
struct pos *route=new struct pos[Path.size()];
int z=0;
while(!Path.empty()){
curr=Path.top();
route[z++]=curr;
Path.pop();
}
for(int k=z-1;k>=0;k--){
cout<<"("<<route[k].i<<","<<route[k].j<<")";
if(k%5==0) cout<<endl;
if(k) cout<<"->";
}
return ;
}
}
cout<<"NO Path!"<<endl;
}
int main()
{
findPath();
system("pause");
return 0;
}
迷宫应该是有解的 但是运行之后结果是无解 求问大神是不是算法哪里有错误 应该怎么改