u010868840 于 2013.10.23 14:11 提问

#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 22:49