迷宫问题完成(初稿)待加注释和优化

来源:互联网 发布:猫 知乎 编辑:程序博客网 时间:2024/06/16 12:18

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
const int MAZE_LENGTH=8;
const int MAZE_WIDTH=8;
const bool TURE=1;
const bool FAULT=0;
const int FAILURE=1;
const int SUCCESS=2;
const int DRIECTION_NORTH=2;
const int DRIECTION_SOUTH=0;
const int DRIECTION_WEST=3;
const int DRIECTION_EAST=1;
const int DRIECTION_FULL=4;
const int MAP_START=2;
const int MAP_END=4;
const int MAP_BAFFLE=1;
const int MAP_EMPTY=0;
const int HISTORY_NO_POINT=0;
const int HISTORY_BEEN_POINT=1;
const int HISTORY_RIGHT_POINT=2;
class CreatMap{
public:
 CreatMap();
 int get_point_map(int ,int ) const;
 int get_point_history(int ,int) const;
 int get_point_direction(int ,int) const;
 void get_start_end(int &,int &);
 bool put_point_history(int ,int , int z=HISTORY_BEEN_POINT);
 bool put_point_direction(int ,int ,int);
 void print_point_map();
private:
 static int point_map[MAZE_LENGTH][MAZE_WIDTH];
 static int point_history[MAZE_LENGTH][MAZE_WIDTH];
 static int point_direction[MAZE_LENGTH][MAZE_WIDTH];
};

int CreatMap::point_map[MAZE_LENGTH][MAZE_WIDTH]={
 {MAP_START,MAP_EMPTY,MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_BAFFLE,MAP_EMPTY},
 {MAP_BAFFLE,MAP_EMPTY,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE},
 {MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_BAFFLE,MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY},
 {MAP_EMPTY,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY},
 {MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY},
 {MAP_EMPTY,MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_BAFFLE,MAP_EMPTY},
 {MAP_EMPTY,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_BAFFLE,MAP_EMPTY},
 {MAP_BAFFLE,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_EMPTY,MAP_END}};
int CreatMap::point_history[MAZE_LENGTH][MAZE_WIDTH]={HISTORY_NO_POINT};
int CreatMap::point_direction[MAZE_LENGTH][MAZE_WIDTH]={DRIECTION_SOUTH};

CreatMap::CreatMap()
{
}

void CreatMap::get_start_end(int &start_x ,int &start_y)
{
 for(int x=0;x<MAZE_LENGTH;x++)
  for(int y=0;y<MAZE_WIDTH;y++)
  {
   if(get_point_map(x,y)==MAP_START)
   {
    start_x=x;
    start_y=y;
   }
  }
}

int CreatMap::get_point_map(int x,int y) const
{
 return point_map[x][y];
}

int CreatMap::get_point_history(int x, int y) const
{
 return point_history[x][y];
}

bool CreatMap::put_point_history(int x, int y , int z)
{
 point_history[x][y]=z;
 return TURE;
}


int CreatMap::get_point_direction(int x, int y) const
{
 return point_direction[x][y];
}
bool CreatMap::put_point_direction(int x, int y, int direction)
{
 
 point_direction[x][y]=direction;
 return TURE;
}
void CreatMap::print_point_map()
{
  cout<<"0表示遍历结果,S表示起点,E表示终点,X表示障碍";
  for(int x=0;x<MAZE_LENGTH;x++)
  {
  cout<<endl;
  cout<<"+---+---+---+---+---+---+---+---+"<<endl;
  for(int y=0;y<MAZE_WIDTH;y++)
  {
   if(point_history[x][y]==HISTORY_RIGHT_POINT) cout<<"| O ";
   else if(point_map[x][y]==MAP_END) cout<<"| E ";
   else if(point_map[x][y]==MAP_START) cout<<"| S ";
   else if(point_map[x][y]==MAP_BAFFLE) cout<<"| X ";
   else  cout<<"|   ";
  }
  cout<<"|";
  }
 cout<<"/n"<<"+---+---+---+---+---+---+---+---+"<<endl;
}


class Node{//堆栈的结点类
public:
 Node();//构造函数0
 Node(int , int , Node *);//构造函数重载1
 void get_xy(int &, int &);//取结点的X,Y值
 void put_xy(int , int );//置结点的X,Y值
 bool put_next(Node *);//置结点的前驱结点域
 bool put_prior(Node *);//置结点的后继结点域
 Node* get_next()const;//取结点的前驱结点域
 Node* get_prior()const;//取结点的后继结点域
private:
 Node *next;
 Node *prior;
 int point_x;
 int point_y;
};
Node::Node()
{
 point_x=NULL;
 point_y=NULL;
 next=NULL;
 prior=NULL;
}
Node::Node(int x, int y, Node *p)
{
 point_x=x;
 point_y=y;
 next=NULL;
 prior=p;
}
void Node::get_xy(int &x, int &y)
{
 x=point_x;
 y=point_y;
}
void Node::put_xy(int x, int y)
{
 point_x=x;
 point_y=y;
}
bool Node::put_next(Node *n)
{
 next=n;
 return TURE;
}
bool Node::put_prior(Node *p)
{
 prior=p;
 return TURE;
}
Node* Node::get_next() const
{
 return next;
}
Node* Node::get_prior() const
{
 return prior;
}

class Stack{//链式堆栈类的实现
public:
 Stack();//基本构造函数
 ~Stack();//构析函数,用于对象删除时释放空间
 bool push(int x, int y);//向堆栈压入新结点及数据
 bool pop(int &x, int &y);//数据出栈,并销毁栈顶结点
 bool destroy_stack();//销毁堆栈,释放空间
 void print_stack();//打印整个结点数据,用于调试
private:
 Node base;//堆栈的栈底结点
 Node *top;//堆栈的栈顶指针,注意,这里的栈顶指针指向的是栈顶的数据结点,非其上面
};
Stack::Stack():base()//堆栈的构造函数,调用结点的构造函数0
{
 top=&base;
}
Stack::~Stack()
{
 destroy_stack();
}
bool Stack::push(int x, int y)
{
 if(top!=NULL)//判断结点是否存在
 {
  if(base.get_next()==NULL)//判断栈底结点是否有数据,注意是是否有数据,因为栈底可以为:有数据且无前驱结点;栈底无数据;栈底有数据且有前驱结点;
  {
  base.put_xy(x,y);
  base.put_next(top);//进入循环说明属于栈底无数据且栈底结点的前驱结点为空,这里置栈底结点的前驱结点为top,使得下次复合栈底有数据且top==base,且不再入此循环
  }
  else
  {
  Node *temp=new Node(x,y,top);//建立新结点,并链接之
  top->put_next(temp);
  top=temp;
  }
 }
 else
 {
  return FAULT;
 }
return TURE;
}
bool Stack::pop(int &x, int &y)//数据出栈
{
 if(base.get_next()!=NULL)//判断栈是否为空和栈底是否有数据存入
 {
  top->get_xy(x,y);
  if(base.get_next()==top)//若符合则说明栈底有数据,且无前驱结点
  {
   base.put_next(NULL);
  }
  else//说明栈底数据不为空,且有前驱结点
  {
   Node *temp=top;
   top=top->get_prior();
   top->put_next(NULL);
   delete temp;//释放空间
  }
 }
 else
 {
  return FAULT;
 }
 return TURE;
}
bool Stack::destroy_stack()
{
 Node *temp1=base.get_next();
 while(temp1&&top!=temp1)
 {
  Node *temp2=temp1->get_next();
  delete temp1;
  temp1=temp2;
 }
 base.put_next(NULL);
 base.put_xy(NULL,NULL);
 return TURE;
}

void Stack::print_stack()
{
 Node *temp;
 int x,y;
 temp=&base;
 while(temp!=NULL)
 {
  temp->get_xy(x,y);
  cout<<x<<" "<<y<<" "<<endl;
  temp=temp->get_next();
 }
}

void main()
{
 int start_x,start_y,now_x,now_y;
 CreatMap map;
 Stack save;
 map.get_start_end(start_x,start_y);
 now_x=start_x;
 now_y=start_y;
 map.print_point_map();
 cout<<"/nPress Any Key To Start!";
 getchar();
 system("cls");
 while(1)
 {
 switch(map.get_point_direction(now_x,now_y))
 {
 case DRIECTION_SOUTH:{
  if(map.get_point_map(now_x+1,now_y)!=MAP_BAFFLE&&map.get_point_history(now_x+1,now_y)==HISTORY_NO_POINT&&now_x<MAZE_LENGTH-1)
  {
   save.push(now_x,now_y);
   map.put_point_direction(now_x,now_y,DRIECTION_EAST);
   map.put_point_history(now_x,now_y);
   now_x++;
   break;
  }

     }
 case DRIECTION_EAST:{
  if(map.get_point_map(now_x,now_y+1)!=MAP_BAFFLE&&map.get_point_history(now_x,now_y+1)==HISTORY_NO_POINT&&now_y<MAZE_WIDTH-1)
  {
   save.push(now_x,now_y);
   map.put_point_direction(now_x,now_y,DRIECTION_NORTH);
   map.put_point_history(now_x,now_y);
   now_y++;
   break;
  }

     }
 case DRIECTION_NORTH:{
  if(map.get_point_map(now_x-1,now_y)!=MAP_BAFFLE&&map.get_point_history(now_x-1,now_y)==HISTORY_NO_POINT&&now_x>0)
  {
   save.push(now_x,now_y);
   map.put_point_direction(now_x,now_y,DRIECTION_WEST);
   map.put_point_history(now_x,now_y);
   now_x--;
   break;
  }

     }
 case DRIECTION_WEST:{
  if(map.get_point_map(now_x,now_y-1)!=MAP_BAFFLE&&map.get_point_history(now_x,now_y-1)==HISTORY_NO_POINT&&now_y>0)
  {
   save.push(now_x,now_y);
   map.put_point_direction(now_x,now_y,DRIECTION_FULL);
   map.put_point_history(now_x,now_y);
   now_y--;
   break;
  }

     }
 default:{
  map.put_point_history(now_x,now_y);
  if(!save.pop(now_x,now_y))
  {
   cout<<"ERROR!!!No Way To Arrival!"<<endl;
   map.print_point_map();
   getchar();
   exit(FAILURE);
  }
  
   }
 }
 if(map.get_point_map(now_x,now_y)==MAP_END)
 {
  while(save.pop(start_x,start_y))
   map.put_point_history(start_x,start_y,HISTORY_RIGHT_POINT);
  map.print_point_map();
 getchar();
 exit(SUCCESS);
 }
 }
}

原创粉丝点击