#include
using namespace std;
template //声明一个类模板
class SqStack
{
public: //顺序栈类的各成员函数
SqStack(int m = 1000);
~SqStack();
void Clear();
bool Empty()const;
int Length()const;
T &Top()const;
void Push(const T &e);
void Pop();
private: //顺序栈类的数据成员
T m_base; //基地址指针
int m_top; //栈顶指针
int m_size; //向量空间大小
};
//构造函数,分配m个结点的顺序空间,构造一个空的顺序栈。
template
SqStack::SqStack(int m)
{
m_top = 0;
m_base = new T[m];
m_size = m;
}
//析构函数,将栈结构销毁。
template
SqStack::~SqStack()
{
if (m_base != NULL)
delete[] m_base;
}
//清空栈。
template
void SqStack::Clear()
{
m_top = 0;
}
//判栈是否为空,若为空,则返回true,否则返回false。
template
bool SqStack::Empty()const
{
return m_top == 0;
}
//求栈的长度。
template
int SqStack::Length()const
{
return m_top;
}
//取栈顶元素的值。先决条件是栈不空。
template
T &SqStack::Top()const
{
return m_base[m_top - 1];
}
//入栈,若栈满,则先扩展空间。插入e到栈顶。
template
void SqStack::Push(const T &e)
{
if (m_top >= m_size)
{
T newbase;
newbase = new T[m_size + 10];
for (int j = 0; j
newbase[j] = m_base[j];
delete[] m_base;
m_base = newbase;
m_size += 10;
}
m_base[m_top++] = e;
}
//入栈,若栈满,则先扩展空间。插入e到栈顶。
template
void SqStack::Pop()
{
m_top--;
}#include"windows.h"
#include"stdlib.h"
#include"time.h"
#include"SqStack.h"
#include "iostream"
using namespace std;
#define MAXSIZE 30
#define N 25
const static int _MAX = 65535;
struct PosType
{
int x;
int y;
};
struct DataType
{
PosType seat;
int di;
};
//输出坐标
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); //定义一个结构体pos,x,y就是构成的成员
}
void color(int b)
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(hConsole, b); //调用API设置字体和背景的颜色函数
}
class Maze
{
protected:
// static int MAX = 1111;
public:
Maze(int, int,bool );
~Maze();
bool MazePath(PosType, PosType,bool);
void Input();
void Print();
void init();
void Manual();
private:
PosType NextPos(PosType, int);
int **m_maze;
int m_row;
int m_col;
bool w;
};
Maze::Maze(int m, int n,bool k)
{
if (k)
{
m_row = m + 2;
m_col = n + 2;
w = k;
m_maze = new int *[m_row];
for (int i = 0; i < m_row; i++)
m_maze[i] = new int[m_col];
this->Input();
}
else
{
int i = 0, j = 0;
m_row = m + 2;
m_col = n + 2;
w = k;
m_maze = new int *[m_row];
for ( i = 0; i < m_row; i++)
m_maze[i] = new int[m_col];
int p[20][20] = {
0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0,
1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0,
1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0,
1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,
};
for (i = 1; i <= m_row - 2; i++)
for (j = 1; j <= m_col - 2; j++)
m_maze[i][j]=p[i-1][j-1];
// m_maze[i][j] = rand() % 2;
for (i = 0; i < m_row; i++)
{
m_maze[i][0] = 65535;
m_maze[i][m_col - 1] = 65535 ;
}
for (j = 1; j < m_col - 1; j++)
{
m_maze[0][j] = 65535;
m_maze[m_row - 1][j] = 65535;
}
}
}
Maze::~Maze()
{
for (int i = 0; i < m_row; i++)
if (m_maze[i] != NULL)
delete[] m_maze[i];
if (m_maze != NULL)
delete[] m_maze;
}
bool Maze::MazePath(PosType start, PosType end,bool m)
{
SqStack path(MAXSIZE);
PosType curpos;
DataType e;
curpos = start;
int curstep = 2;
e.di = 0;
do {
if (m_maze[curpos.x][curpos.y] == 0)
{
m_maze[curpos.x][curpos.y] = curstep;
e.seat.x = curpos.x;
e.seat.y = curpos.y;
path.Push(e);
curstep++;
if (m)
{
Sleep(50);
gotoxy(0,0);
this->init();
}
if (curpos.x == end.x && curpos.y == end.y)
{
m_maze[curpos.x][curpos.y] = 2;
return true;
}
curpos = NextPos(curpos, e.di);
}
else
{
if (!path.Empty())
{
e = path.Top();
path.Pop();
curstep--;
while (e.di == 3&&!path.Empty())
{
m_maze[e.seat.x][e.seat.y] = -1;
e = path.Top();
path.Pop();
curstep--;
}
if (e.di < 3)
{
if(m_maze[e.seat.x+1][e.seat.y]==0&&m_maze[e.seat.x-1][e.seat.y]!=0&&m_maze[e.seat.x][e.seat.y+1]!=0&&m_maze[e.seat.x][e.seat.y-1]!=0)
e.di=0;
if(m_maze[e.seat.x+1][e.seat.y]!=0&&m_maze[e.seat.x-1][e.seat.y]==0&&m_maze[e.seat.x][e.seat.y+1]!=0&&m_maze[e.seat.x][e.seat.y-1]!=0)
e.di=1;
if(m_maze[e.seat.x+1][e.seat.y]!=0&&m_maze[e.seat.x-1][e.seat.y]!=0&&m_maze[e.seat.x][e.seat.y+1]==0&&m_maze[e.seat.x][e.seat.y-1]!=0)
e.di=2;
if(m_maze[e.seat.x+1][e.seat.y]!=0&&m_maze[e.seat.x-1][e.seat.y]!=0&&m_maze[e.seat.x][e.seat.y+1]!=0&&m_maze[e.seat.x][e.seat.y-1]==0)
e.di=3;
else
{
e.di =rand()%3;
}
curstep++;
path.Push(e);
curpos = NextPos(e.seat, e.di);
}
}
}
} while (!path.Empty());
return false;
}
void Maze::Input()
{
int i, j;
cout << "请输入" << m_row-2 << "*" << m_col - 2 << "的迷宫:" << endl;
for (i = 1; i <= m_row - 2; i++)
for (j = 1; j <= m_col - 2; j++)
cin >> m_maze[i][j];
for (i = 0; i < m_row; i++)
{
m_maze[i][0] = 65535;
m_maze[i][m_col - 1] = 65535;
}
for (j = 1; j < m_col - 1; j++)
{
m_maze[0][j] = 65535;
m_maze[m_row - 1][j] = 65535;
}
}
void Maze::Print()
{
//FILE *fp;
//errno_t err;
// err = fopen_s(&fp, "output", "w");
// fprintf(fp, "----------迷宫路径----------\n");
int i, j;
for (i = 0; i < m_row; i++)
{
for (j = 0; j < m_col; j++)
{
cout << " " << m_maze[i][j];
// fprintf(fp, "\t%d", m_maze[i][j]);
}
cout << endl;
// fprintf(fp, "\n");
}
// fprintf(fp, "\n");
// fclose(fp);
}
PosType Maze::NextPos(PosType c, int d)
{
PosType direct[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
c.x += direct[d].x;
c.y += direct[d].y;
return c;
}
//界面初始化
void Maze::init() //初始化一个迷宫
{
for (int i = 0; i < m_row; i++)
{
for (int j = 0; j < m_col; j++)
{
if (m_maze[i][j]==1)
{
color(11);
cout << "■"; //围墙或者此路不通
}
else if (m_maze[i][j] ==65535)
{
color(10);
cout << "◇";
}
else if (m_maze[i][j]==0)
{
color(12);
cout << "□"; //通路
}
else if (m_maze[i][j] == 2)
{
color(15);
cout << "★";
}
else
{
color(14);
cout << "★";
}
}
cout << endl;
}
}
void Maze::Manual()
{
gotoxy(N + 25, 1);
color(11);
cout<<"现在呈现一个"<<m_row-2<<"*"<<m_col-2<<"迷宫";
}
void main()
{
srand((unsigned)time(NULL));
while (1)
{
PosType begin, end;
bool k;
cout << "请选择模式:" << endl;
cout << "如果要自己输入迷宫,输入非0数,如果要想用现成的输入0:" << endl;
cout << "请选择!!!";
cin >> k;
int m, n;
cout << "请输入迷宫的行数,列数:";
cin >> m >> n;
Maze maze(m, n, k);
system("cls");
maze.init();
maze.Manual();
gotoxy(N + 25, 7);
cout << "出口和入口行数和列数需加1 \n";
gotoxy(N + 25, 8);
cout << "请输入入口位置(行数,列数):";
cin.sync();
gotoxy(N + 25, 9);
cin >> begin.x >> begin.y;
gotoxy(N + 25, 13);
cout << "请输入出口位置(行数,列数):";
gotoxy(N + 25, 14);
cin >> end.x >> end.y;
bool l;
gotoxy(N + 25, 16);
cout << "是否想看走迷宫过程?0否1是!";
gotoxy(N + 25, 17);
cin >> l ;
if (maze.MazePath(begin, end, l))
{
if (!l)
gotoxy(0, N + 25);
cout << "此迷宫从入口到出口的一条路径如下:" << endl;
maze.init();
}
else
{
if (!l)
gotoxy(0, N + 25);
cout << "此迷宫没有从入口到出口的路径" << endl;
}
cin.sync();
system("pause");
}
}