迷宫

//此为多文件结构

//base.h

#pragma once

#include <iostream>
#include <tchar.h>
#include <string.h>

using namespace std;

const int ROW = 10;//矩阵行数
const int COL = 8;//矩阵列数

//stack.h

#pragma once

#include "base.h"
//#include "maze.h"


const int MAX_STACK_SIZE = 15;//栈的初始分配量
const int ADD_STACK_SIZE = 5;//新增的分配量

//坐标位置
struct PosType
{
 int row;//横坐标
 int col;//纵坐标
};
//栈的元素类型
typedef struct Maze_Stack_Elem
{
 int current_order;//当前位置在路径上的“序号“
 PosType seat;//当前位置在迷宫中的“坐标位置”
 int next_direction;//往下一个通道块的“方向”
}*Stack_Elem;//元素类型

//栈的类型
struct Maze_Stack
{
 Stack_Elem *base;//栈底指针
 Stack_Elem *top;//栈顶指针
 int stack_size;//栈的容量
};//栈的类型
/***************元素操作***************/
//创建元素并赋值
void Init_Elem(Stack_Elem &stack_elem,int current_step,PosType &current_pos,int nextdirection)
{
 stack_elem = new Maze_Stack_Elem;
 stack_elem->current_order = current_step;
 stack_elem->seat = current_pos;
 stack_elem->next_direction = nextdirection;
}
//为元素赋值
/*void Set_Elem(Stack_Elem &stack_elem,int current_step,PosType &current_pos,int nextdirection)
{
 stack_elem->current_order = current_step;
 stack_elem->seat = current_pos;
 stack_elem->next_direction = nextdirection;
}*/

/***************栈的基本操作******************/

//栈的创建
void Create_Stack(Maze_Stack &stack)
{
 stack.base = new Stack_Elem[MAX_STACK_SIZE];
 stack.top = stack.base;
 stack.stack_size = MAX_STACK_SIZE;
}

//取栈顶元素
bool Get_Top(Maze_Stack &stack,Stack_Elem &stack_elem)
{
 if(stack.top == stack.base)
  return false;
 Stack_Elem *p; p= stack.top;
 p++;
 stack_elem = *p;
 return true;
}

//判断栈是否为空
bool Stack_Empty(Maze_Stack &stack)
{
 if(stack.base == stack.top)
  return true;
 return false;
}

//入栈
void Push(Maze_Stack &stack,Stack_Elem &stack_elem)
{
 if((stack.top - stack.base)>=stack.stack_size)
 {
  Maze_Stack newstack;
  newstack.base = new Stack_Elem[stack.stack_size + ADD_STACK_SIZE];
  if(newstack.base != NULL)
  {
   newstack.top = newstack.base;
   stack.top = stack.base;
   while((stack.top-stack.base)<stack.stack_size)
   {
    newstack.top = stack.top;
    newstack.top++;
    stack.top++;
   }
   delete stack.base;
   stack.base=newstack.base;
   stack.top = newstack.top;
   stack.stack_size += ADD_STACK_SIZE;
   newstack.base = NULL;
   newstack.top = NULL;
   newstack.stack_size = 0;
   *stack.top = stack_elem;
   stack.top++;
  }
 }
 else
 {
  *stack.top = stack_elem;
  stack.top++;
 }

}

//出栈
void Pop(Maze_Stack &stack,Stack_Elem &stack_elem)
{
 if(!Stack_Empty(stack))
 {
  stack.top--;
  stack_elem = *stack.top;
 }
 else
  stack_elem = NULL;
}

//打印栈记录
void Stack_Print(Maze_Stack &stack)
{
 Stack_Elem elem;
 Stack_Elem *_printer;    _printer = stack.base;

 cout<<endl;
 while(_printer < stack.top)
 {
  elem = *_printer;
  cout<<elem->seat.row <<" "<<elem->seat.col<<endl;
  _printer++;
 }
}
//清空
void Clear_Stack(Maze_Stack &stack)
{
 stack.top = stack.base;
}
//销毁
void Destory_Stack(Maze_Stack &stack)
{
 delete stack.base;
 stack.base = NULL;
 stack.top = NULL;
}

//maze.h 

#include "base.h"
#include "stack.h"

const int MAX_ROW = 50;//最大行数
const int MAX_COL = 50;//最大列数

//迷宫类型
struct Maze_Type
{
 int m;//横坐标
 int n;//纵坐标
 int _maze[MAX_ROW][MAX_COL];//
};//迷宫类型

//创建迷宫
void Init_Maze(Maze_Type &maze,int _smaze[][COL],const int row,const int col)
{

 for(maze.m = 0;maze.m<row;)
 {
  for(maze.n = 0;maze.n<col;)
  {
   maze._maze[maze.m][maze.n] = _smaze[maze.m][maze.n];
   maze.n++;
  }
  maze.m++;
 }
}

void Init_Maze(Maze_Type &maze,const int row,const int col)
{
 for(maze.m = 0;maze.m<row;maze.m++)
 {
  for(maze.n = 0;maze.n<col;maze.n++)
  {
   cin>>maze._maze[maze.m][maze.n];
  }
 }
}
//判断当前位置是否可以通过
bool Pass(Maze_Type  &maze,PosType &current_pos)
{
 if(maze._maze[current_pos.row][current_pos.col] == 0)
  return true;
 else
  return false;
}

//留下足迹
void Foot_Print(Maze_Type &maze,PosType &current_pos)
{
 maze._maze[current_pos.row][current_pos.col] = 8;
}

 //留下不能通过的标记
void Mark_Print(Maze_Type &maze,PosType &current_pos)
{
 maze._maze[current_pos.row][current_pos.col] = 9;
}

//向下一个位置探索
PosType Next_Pos(PosType &current_pos,int nextdirection)
{
 switch(nextdirection)
 {
 case 1://东
  current_pos.col++;
  break; 
 case 2://南
  current_pos.row++;
  break;
 case 3://西
  current_pos.col--;
  break;
 case 4://北
  current_pos.row--;
  break;
 default:
  break;
 }
 return current_pos;
}

//走迷宫
bool Maze_Path(Maze_Type &maze,PosType &start,PosType &end)
{
 Maze_Stack stack;
 Stack_Elem stack_elem;
 PosType current_pos;//当前位置
 current_pos = start;//设当前位置为入口位置
 int current_step = 1;//探索第一步
 Create_Stack(stack);
 while(1)
 {
  if(Pass(maze,current_pos))
  {
   Foot_Print(maze,current_pos);
   Init_Elem(stack_elem,current_step,current_pos,1);
   Push(stack,stack_elem);
   if((current_pos.row == end.row)&&(current_pos.col == end.col))
   {
    Stack_Print(stack);
    return true;
   }
   current_pos = Next_Pos(current_pos,1);
   current_step++;//探索下一步
  }//if
  else
  {
   if(!Stack_Empty(stack))
   {
    Pop(stack,stack_elem);
    while(stack_elem->next_direction == 4 && !Stack_Empty(stack))
    {
     Mark_Print(maze,stack_elem->seat);     
     Pop(stack,stack_elem);
     current_step--;
    }//while
    if(stack_elem->next_direction < 4)
    {
     stack_elem->next_direction++;
     Push(stack,stack_elem);
     PosType newcur_pos;
     newcur_pos = stack_elem->seat;
     current_pos =Next_Pos(newcur_pos,stack_elem->next_direction);
    }//if    
   }//if
  }//else
  if(Stack_Empty(stack))
   break;
 }
 return false;
}

 

//maze_main.cpp


#include "maze.h"

int _tmain(int argc,_TCHAR* argv[])
{
 int max_row= 0,max_col = 0;//矩阵行列
 PosType start,end;//出口和入口
 start.row = 3;start.col = 3;
 end.row = 4;end.col = 6;

 //测试所用数据
 /*int _smaze[ROW][COL] ={
 {1,1,1,1,1,1,1,1},
 {1,0,1,1,0,1,0,1},
 {1,0,1,0,0,1,0,1},
 {1,1,0,0,1,0,1,1},
 {1,0,0,1,0,0,0,1},
 {1,0,0,0,0,1,1,1},
 {1,0,1,0,0,1,0,1},
 {1,0,1,0,0,0,1,1},
 {1,1,1,1,0,0,0,1},
 {1,1,1,1,1,1,1,1}};
 
 Maze_Type maze;
 Init_Maze(maze,_smaze,ROW,COL);
 for(int i=0;i<10;i++)
 {
  for(int j = 0;j<8;j++)
  {
   if(j%8==0)
    std::cout<<std::endl;
   std::cout<<maze._maze[i][j]<<"/t";
  }
 }
 cout<<endl;
 if(!Maze_Path(maze,start,end))
  cout<<"找不到路径!"<<endl;*/

 cout<<"请输入行列数:"<<endl;
 cin>>max_row>>max_col;
 if(max_row > 0 && max_col > 0)
 {
  Maze_Type maze;
  Init_Maze(maze,max_row,max_col);
  if(!Maze_Path(maze,start,end))
   cout<<"找不到路径!"<<endl;

  /*for(int i=0;i<10;i++)
  {
   for(int j = 0;j<8;j++)
   {
    if(j%8==0)
     std::cout<<std::endl;
    std::cout<<maze._maze[i][j]<<"/t";
   }
  }
  cout<<endl;

  for(int i=0;i<10;i++)
  {
   for(int j = 0;j<8;j++)
   {
    if(j%8==0)
     std::cout<<std::endl;
    std::cout<<maze._maze[i][j]<<"/t";
   }
  }
  cout<<endl;*/
 }
 return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值