数据结构与算法-实验3-自定义栈,并实现走迷宫问题
2016-11-07 09:39
573 查看
#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]
e469
[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;
}
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]
e469
[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;
}
相关文章推荐
- 数据结构与算法-实验2-自定义栈,并实现push、pop、改栈顶元素、取栈顶元素
- 数据结构与算法实验题 11.3 最小权语言问题
- C语言 数据结构中求解迷宫问题实现方法
- 数据结构与算法实验题 11.1 堆箱子问题
- 数据结构实验(严蔚敏版)2.9迷宫问题
- [算法]数据结构算法背包问题解法之递归解法,C语言实现
- (2013-4-21)数据结构实验三:狐狸逮兔问题(方法一:顺序表实现)
- 【并查集】数据结构与算法实验题 11.2 病毒排查问题
- 数据结构与算法实验题 11.3 最小权语言问题
- 数据结构与算法学习之路:迷宫问题——回溯思想找出所有路径
- 【并查集】数据结构与算法实验题 11.3 培养箱分配问题
- 第六周--数据结构之自建算法库之迷宫问题(用栈结构)
- 第六周--数据结构之自建算法库之迷宫问题(用队列)
- 数据结构作业 迷宫问题 模拟 之基础的BFS DFS
- 数据结构课程设计——迷宫问题课程设计报告
- 【数据结构与算法】第三章 表c实现应用一-----------多项式
- 【数据结构与算法基础】以数组实现的循环队列 / Circular Queue implemented by array
- 数据结构实验四:图的表示和实现
- 数据结构和算法设计(迷宫求解问题的栈和队列的实现)
- 【数据结构与算法】左偏树(堆)的实现