#include <iostream>
using namespace std;
const int maxnum = 100;
typedef struct Datastack
{
int num;
int x, y;
struct Datastack * pre, * next;
}datastack;
datastack *Create_stack();
datastack *push_stack(datastack *p, int x1, int y1); //进栈
datastack *pop_stack(datastack *p); //出栈
bool Isempty_stack(datastack *p); //判栈空
bool Isfull_stack(datastack *p); //判栈满
bool IsExit(datastack *p); //判是否已达出口
datastack *judge_kernel(datastack *p, int s[12][12]); //评判核心
int main()
{
datastack *head = NULL; //栈头指针
cout << "Create_stack() 正在创建栈区域... ";
head = Create_stack();
if (head != NULL)
{
cout << "创建栈区域成功!\n";
}
else
{
cout << "创建栈区域失败!\n";
return 0;
}
int M[12][12] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, //起始点为(1,1)
1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1,
1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1,
1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, //终止点为(10,10)
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
//打印起始迷宫图
cout << "The Enter is at (1,1):\n";
for (int i = 0; i < 12; i++)
{
if (i == 1)
{
cout << "Enter-->";
}
else
{
cout << " ";
}
for (int j = 0; j < 12; j++)
{
if (j == 11)
{
cout << M[i][j];
}
else
{
cout << M[i][j] << " ";
}
}
if (i == 10){ cout << "-->Exit"; }
cout << endl;
}
head = judge_kernel(head, M); //开始评判
if (Isempty_stack(head))
{
cout << "No Path!No Gate!\n"; //不存在这样的路径
}
else
{
cout << "Path: "; //打印出路径
datastack *head2 = head->pre;
for (int a = 0; a <( head->num)/5+1; a++)
{
if (a >= 1)
{
cout << " ";
}
for (int b = 0; b < 5; b++)
{
cout << "maze[" << head2->x << "][" << head2->y << "],";
if (head2->next == NULL){ break; }
head2 = head2->next;
}
cout << endl;
//if (head2->next == NULL){ break; }
}
cout << "Path Length: " << head->num << endl; //打印路径长度
head2 = NULL;
cout << "One Path: \n"; //打印出路径
//打印带路径的迷宫图
for (int i = 0; i < 12; i++)
{
if (i == 1)
{
cout << "Enter-->";
}
else
{
cout << " ";
}
for (int j = 0; j < 12; j++)
{
if (M[i][j] == 0 || M[i][j] == 1)
{
if (i == 10 && j == 11)
{
cout << M[i][j];
}
else
{
cout << M[i][j] << " ";
}
}
else if (M[i][j]==2)
{
cout << "0"<<" ";
}
else if (M[i][j] == 3)
{
cout <<"x"<< " ";
}
}
if (i == 10){ cout << "-->Exit"; }
cout << endl;
}
}
while (!Isempty_stack(head))
{
head = pop_stack(head); //清空栈,回收空间
}
free(head);
return 0;
}
//核心评判
datastack *judge_kernel(datastack *p, int s[12][12])
{
p = push_stack(p, 1, 1); //起始点坐标进栈
s[((p->next)->x)][((p->next)->y)] = 3; //表示元素在栈中,只能进行退栈操作
while (!Isempty_stack(p))
{
if (IsExit(p)){ break; } //是否已找到出口点
if (s[((p->next)->x)][((p->next)->y) + 1] == 0)
{
s[((p->next)->x)][((p->next)->y) + 1] = 3;
p = push_stack(p, ((p->next)->x), ((p->next)->y) + 1); //向右移动
}
else if (s[((p->next)->x) + 1][((p->next)->y)] == 0)
{
s[((p->next)->x) + 1][((p->next)->y)] = 3;
p = push_stack(p, ((p->next)->x) + 1, ((p->next)->y)); //向下移动
}
else if (s[((p->next)->x) - 1][((p->next)->y)] == 0)
{
s[((p->next)->x) - 1][((p->next)->y)] = 3;
p = push_stack(p, ((p->next)->x) - 1, ((p->next)->y)); //向上移动
}
else if (s[((p->next)->x) - 1][((p->next)->y) + 1] == 0)
{
s[((p->next)->x) - 1][((p->next)->y) + 1] = 3;
p = push_stack(p, ((p->next)->x) - 1, ((p->next)->y) + 1); //向右上角移动
}
else if (s[((p->next)->x) + 1][((p->next)->y) + 1] == 0)
{
s[((p->next)->x) + 1][((p->next)->y) + 1] = 3;
p = push_stack(p, ((p->next)->x) + 1, ((p->next)->y) + 1); //向右下角移动
}
else if (s[((p->next)->x) + 1][((p->next)->y) - 1] == 0)
{
s[((p->next)->x) + 1][((p->next)->y) - 1] = 3;
p = push_stack(p, ((p->next)->x) + 1, ((p->next)->y) - 1); //向左下角移动
}
else if (s[((p->next)->x)][((p->next)->y) - 1] == 0)
{
s[((p->next)->x)][((p->next)->y) - 1] = 3;
p = push_stack(p, ((p->next)->x), ((p->next)->y) - 1); //向左移动
}
else if (s[((p->next)->x) - 1][((p->next)->y) - 1] == 0)
{
s[((p->next)->x) - 1][((p->next)->y) - 1] = 3;
p = push_stack(p, ((p->next)->x) - 1, ((p->next)->y) - 1); //向左上角移动
}
else
{
s[((p->next)->x)][((p->next)->y)] = 2; //标记退栈元素
p = pop_stack(p); //退栈
}
}
return p;
}
//创建栈
datastack *Create_stack()
{
datastack *p = NULL;
datastack *s = NULL;
s = (datastack *)malloc(sizeof(datastack)); //仅创建栈计数节点
s->pre = NULL;
s->num = 0;
s->next = NULL;
p = s;
return p;
}
//进栈
datastack *push_stack(datastack *p, int x1, int y1)
{
datastack *s = NULL;
if (Isfull_stack(p)) //判栈满
{
cout << "栈已满,入栈操作失败!\n";
return p;
}
if (p->num == 0)
{
s = (datastack *)malloc(sizeof(datastack)); //新节点空间
s->pre = p;
s->x = x1;
s->y = y1;
s->next = NULL;
p->pre = s; //方便后续打印路径数组
p->next = s;
p->num++;
}
else
{
s = (datastack *)malloc(sizeof(datastack)); //新节点空间
s->pre = p->next;
s->x = x1;
s->y = y1;
s->next = NULL;
(p->next)->next = s;
p->next = s;
p->num++;
}
return p;
}
//出栈
datastack *pop_stack(datastack *p)
{
datastack *pfree = NULL;
if (Isempty_stack(p)) //判栈空
{
cout << "栈已空,出栈操作失败!\n";
return p;
}
pfree = p->next;
p->next = pfree->pre;
(p->next)->next = NULL;
pfree->pre = NULL;
if (p->pre == pfree){ p->pre = NULL; }
free(pfree);
p->num--;
return p;
}
//判是否已达出口点
bool IsExit(datastack *p)
{
if (Isempty_stack(p)) //判栈空
{
cout << "栈已空,没有到达出口点!\n";
return false;
}
if ((p->next)->x == 10 && (p->next)->y == 10)
{
return true; //已到达出口点
}
return false;
}
//判栈满
bool Isfull_stack(datastack *p)
{
if (p->num == maxnum)
{
return true;
}
return false;
}
//判栈空
bool Isempty_stack(datastack *p)
{
if (p->num == 0)
{
return true;
}
return false;
}