// 迷宫2.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
struct stack
{
int x;
int y;
int d[4];
stack* next;
};
stack* inti()
{
stack *s = new stack();
s->next = NULL;
for(int i=0;i<4;i++)
s->d[i] = 0;
if (!s)
cout << "error!!!";
return s;
}
//static stack * shorts = inti();
void push(stack *s, int x, int y)
{
stack *t = inti();
t->x = x;
t->y = y;
for(int i=0;i<4;i++)
t->d[i] = 0;
t->next = s->next;
s->next = t;
}
stack* pop(stack* s)
{
stack* t = inti();
if (s->next)
{
t->x = s->next->x;
t->y = s->next->y;
for (int i = 0; i < 4; i++)
{
t->d[i]=s->next->d[i];
}
stack* n = s->next;
s->next = n->next;
delete n;
}
else
cout << "error";
return t;
}
void deletestack(stack* s)
{
while (s)
{
delete pop(s);
}
}
//计算路径的长度
int length(stack *s)
{
stack * t = s->next;
int i = 0;
while (t != NULL)
{
t = t->next;
i++;
}
return i;
}
//输出路径
void print(stack *s) //此处经常犯错
{
static int stacknum = 0;
stack *t=s->next;
//if (length(shorts) > length(s))
//{
// deletestack(shorts);
// while (t != NULL)
// {
// push(shorts, t->x, t->y);
// t = t->next;
// }
//}
/*t = s->next;*/
cout << "第 "<<++stacknum <<" 个是:" << endl;
while (t != NULL)
{
cout << "(" << t->x << "," << t->y << ")" << endl;
t = t->next;
}
cout << "the length is " << length(s) << endl;
}
//最短路径
//void printshort()
//{
// stack * t = shorts->next;
// while (t)
// {
// cout << "(" << t->x << "," << t->y << ") ";
// t = t->next;
// }
//}
stack * top(stack *s)
{
stack *t = pop(s);
push(s, t->x, t->y);
return t;
}
//打印地图 查看地图出现的变化
void map(int a[][10])
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << a[i][j];
}
cout << endl;
}
cout << endl;
//getchar();
}
//判断(x,y)周围的路径是否为通
void is_null(int a[10][10], int x, int y, stack *s)
{
if (a[x][y + 1] == 0)
{
s->next->d[0] = 1;
}
if (a[x + 1][y] == 0)
{
s->next->d[1] = 1;
}
if (a[x][y - 1] == 0)
{
s->next->d[2] = 1;
}
if (a[x - 1][y] == 0)
{
s->next->d[3] = 1;
}
}
//走迷宫 将路径寄存在栈中
void zou(int a[10][10], int x, int y, int m, int n, stack*s)
{
push(s, x, y);
a[x][y] = 2;
/*map(a);*/
if (x == m && y == n) //走到终点 输出所有路径
{
print(s);
cout << endl;
/*getchar();*/
}
is_null(a, x, y, s); //判断周围的路径是否为通
//从右到左顺时针递归调用
if (s->next->d[0] == 1)
{
s->next->d[0] = 0;
zou(a, x, y + 1, m, n, s);
}
if (s->next->d[1] == 1)
{
s->next->d[1] = 0;
zou(a, x + 1, y, m, n, s);
}
if (s->next->d[2] == 1)
{
s->next->d[2] = 0;
zou(a, x, y - 1, m, n, s);
}
if (s->next->d[3] == 1)
{
s->next->d[3] = 0;
zou(a, x - 1, y, m, n, s);
}
stack *t =pop(s); //走到尽头 出栈返回上一个周围还有方向的节点
a[t->x][t->y] = 0; //将走过的节点由2重置为0(未走过)
//map(a);
}
int main()
{
stack *s=inti();
int a[10][10] =//入口 (1,8) 出口 (8,4)
{
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,0,0,0,1,
1,1,0,0,0,0,0,0,1,1,
1,1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,1,0,0,1,
1,1,0,1,0,0,0,0,1,1,
1,1,0,1,1,0,1,1,0,1,
1,1,0,1,1,0,1,1,0,1,
1,1,0,0,0,0,1,0,1,1,
1,1,1,1,1,1,1,1,1,1
};
map(a);
cout << "please input the entrence and the exit of the maze" << endl;
int entren_x, entren_y, exit_x, exit_y;
cout << "please input the entrence's coordinate:";
cin >> entren_x>>entren_y;
cout << "please input the exit's coordinate:";
cin >> exit_x >> exit_y;
cout << "all way from entrence to exit is :" << endl;
zou(a, entren_x, entren_y, exit_x, exit_y, s);
//cout << "the shortest way is:" << endl;
//printshort();
cout << "input 'n' to exit:";
while (getchar() != 'n')
{ }
return 0;
}