迷宫求解问题

来源:互联网 发布:消原唱制伴奏软件 编辑:程序博客网 时间:2024/06/16 19:52

#ifndef _linkstacknode_H
#define _linkstacknode_H
class linkstacknode
{
 friend class linkstack;
 friend class maze;
public :
 linkstacknode(int newx,int newy,int newd);
private:
 int x;
 int y; 
 int d;         //表示走到下一坐标的方向
 linkstacknode *next;
};
#endif

 

#ifndef _linkstack_H
#define _linkstack_H
class linkstack
{
public : 
 linkstack();      
 bool isempty();       //确定栈是否为空
 bool push(linkstacknode *newnode);    
 linkstacknode* pop();
 void printstack();      //打印栈的内容
 void returnstacklength();    //栈的大小
 linkstacknode *top;
private:
 
 int length;        //栈的大小
};
#endif

 

#include<iostream>
#include<iomanip>
#include<conio.h>
#include<time.h>
#include<fstream>
#include"linkstacknode.h"
#include"linkstack.h"
using namespace std;
fstream file("test.txt");
int m,n;         //地图的大小  及行与列
int map[100][100];       //定义一个10*10的地图
int mark[100][100];       //标记数组
int walk[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int curstep=1;//当前走的步数


class maze         
{
public:
 void paintFrame(int m,int n);  //随机产生绘制地图 0表示通过  1表示有障碍
 void showMap();      //显示地图
 bool seekPath(int x,int y);   //寻找路径函数
 void userPaintFrame(int m,int n); //用户自己设定地图
 int  findpath(int x,int y); //递归方法偏历找出所有的解
private:
 linkstack stack;
};
linkstacknode:: linkstacknode(int newx,int newy,int newd)
{
 x=newx;
 y=newy;
 d=newd;
 next=NULL;
}
linkstack::linkstack()

 length=0;
// top=new linkstacknode(1,1,-1);   //构造栈的头结点
 top=NULL;
}
bool linkstack::isempty()
{
// cout<<top->x<<"----"<<endl;
 if(top==NULL)
 {
  return true;
 }
 return false;
}
void linkstack::returnstacklength()
{
 cout<<"length="<<length<<endl;
}
bool linkstack::push(linkstacknode *newnode) 
{
 if(newnode)
 { 
 // cout<<"进栈数据"<<newnode->x<<"===="<<newnode->y<<"====="<<newnode->d<<endl;
  newnode->next=top;
  top=newnode;
  length++;
  return true;
 }
 return false;
}
linkstacknode* linkstack::pop()
{
 linkstacknode *tail;
 if(!isempty())
 {
  tail=top;
  top=tail->next;
 // cout<<"出栈数据"<<tail->x<<endl;
  mark[tail->y][tail->x]=0;
  return tail;   
  delete tail;
  length--;
  
 }
 cout<<"空栈"<<endl;
 return NULL;
 
}

void linkstack::printstack()
{
 linkstacknode *p;
 p=top;
 while(p)
 {
  cout<<p->x<<"---"<<p->y<<"-----"<<p->d<<endl;
  p=p->next;
 }
}
void maze::showMap()
{

}
void maze::userPaintFrame(int m,int n)
{
 int i=0,j=0;
 cout<<"用户自己设置地图(从文件中读取地图信息)!"<<endl;
 for(i=0;i<m+2;i++){
  for(j=0;j<n+2;j++){
   if(i==0||j==0||i==m+1||j==n+1){
    map[i][j]=2;
   }
   else{
    file>>map[i][j];
   }
  }
 }
 mark[1][1]=0;
 showMap(); 
 file.close();
}
void maze::paintFrame(int m,int n)
{
 int i=0,j=0;
 srand((unsigned)time(NULL));
 for(i=0;i<m+2;i++){
  for(j=0;j<n+2;j++){
   if(i==0||j==0||i==m+1||j==n+1){
    map[i][j]=2;
   }
   else{
    map[i][j]=rand()%2;
   }
  }
 }
 mark[1][1]=0;
 map[1][1]=0;        
 map[m][n]=0;
 showMap();
}

//找一条路径
bool  maze::seekPath(int g,int h)
{
 linkstacknode *newnode;
 linkstacknode *temp;
 int x,y,row,col,direction;
 newnode=new linkstacknode(g,h,-1);
 map[g][h]=-1;
 stack.top=newnode;
 while(!stack.isempty())
 {
  temp=stack.pop();
  x=temp->x; y=temp->y;  direction=temp->d+1;
  while(direction<4)
  {
   row=x+walk[direction][0]; col=y+walk[direction][1];
   if(map[row][col]==0){
    newnode=new linkstacknode(x,y,direction);
    stack.push(newnode);
    x=row;  y=col;  map[x][y]=-1;
    if(x==m&&y==n) {
     newnode=new linkstacknode(x,y,direction);
     stack.push(newnode);
     stack.printstack();
     return true;
    }
    else   direction=0;
   }
   else
    direction++;
  }
 }
 return false;
}
//递归求解
int maze::findpath(int x,int y)
{
 if(x==1&&y==1)
 {
  map[x][y]=3;
  return 1;
 }
 else
  if(map[x][y]==0)
  {
   map[x][y]=-1;
   if(findpath(x-1,y)+findpath(x+1,y)+findpath(x,y-1)
    +findpath(x,y-1)>0)
    return 1;
   else
   {
    map[x][y]=0;
    return 0;
   }
  }
 return 0;
}

int main()
{
 system("color f0");
 file>>m>>n;
 maze Maze;
 Maze.userPaintFrame(m,n);
// Maze.seekPath(1,1);
 Maze.findpath(m,n);
 for(int i=1;i<=m;i++)
 {
  for(int j=1;j<=n;j++)
  {
   cout<<map[i][j];
  }
  cout<<endl;
 }
 _getch();
 return 0;
}

 

 

1 0
原创粉丝点击