//此为多文件结构
//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 ¤t_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 ¤t_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 "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;
}