u010868840 2013-10-23 06:11 采纳率: 0%
浏览 1702

初学递归,有些疑惑请大家帮助

是利用递归解决迷宫问题
初学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;
}

感激不尽

  • 写回答

1条回答

  • fanfan_gg 2013-10-23 14:49
    关注

    骚年 使用编辑器里的代码段贴代码吧 这缩进看的蛋疼啊

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行