问题描述
- 初学递归,有些疑惑请大家帮助
-
是利用递归解决迷宫问题
初学C++,要利用递归解决迷宫问题,迷宫需要自己输入,0代表可以走通,1代表走不通,下面是我的程序,求各位看看那里有问题,为什么不出结果
#include
#include
using namespace std;
struct point
{ int x,y;
};
int xmove[4]={0,1,0,-1};
int ymove[4]={1,0,-1,0};
class Maze
{
public:
Maze(int r,int c);
~Maze();
void Buildmaze();
int Seekpath(point p1,point p2);
void Run();
private:
int row,column;
point entrance,exit;
int **maze;
int **mark;
};
Maze::Maze(int r, int c):row(r),column(c)
{}
Maze::~Maze()
{ for(int i=0;i
{
delete []maze;
delete []mark;
maze=NULL;
mark=NULL;
}
delete [row]maze;
maze=NULL;
delete [row]mark;
mark=NULL;
}
void Maze::Buildmaze()
{ cin>>entrance.x>>entrance.y>>exit.x>>exit.y;
maze=new int*[row];
int i,j;
for(i=0;i
maze=new int[column];
for(i=0;i
for(j=0;j
cin>>maze[j];
maze[entrance.x][entrance.y]=0;
}
int Maze::Seekpath(point p1,point p2)
{ int i,j;
point nextpoint;
mark=new int*[row];
for(i=0;i
mark=new int[column];
for(i=0;i
for(j=0;j
mark[j]=0;
mark[entrance.x][entrance.y]=1;
if(p1.x==p2.x&&p1.y==p2.y)
return 1;
for(i=0;i
{ nextpoint.x=p1.x+xmove;
nextpoint.y=p1.y+ymove;
if(nextpoint.x>=0&&nextpoint.x<=row-1&&nextpoint.y>=0&&nextpoint.y<=column-1)
{ if(maze[nextpoint.x][nextpoint.y]==0&&mark[nextpoint.x][nextpoint.y]==0)
{ mark[nextpoint.x][nextpoint.y]=1;
if(Seekpath(nextpoint,exit))
{
return 1;
}
}
}
}
if(i=4)
cout<<"no path"<
return 0;
}
void Maze::Run()
{ Buildmaze();
if(Seekpath(entrance,exit))
{ cout
}
int main()
{ int row,column;
point entrance,exit;
cin>>row>>column;
Maze m(row,column);
m.Run();
m.~Maze();
return 0;
}感激不尽
解决方案
骚年 使用编辑器里的代码段贴代码吧 这缩进看的蛋疼啊
时间: 2024-10-30 14:11:54