// 这是 一个心酸的开头语 用栈来尝试写迷宫算法是一种勇气 放着好好的bfs dfs递归算法俺不用。。。 55555
// 这一切 只能 说 这个 坑B的教材。。。
#include<iostream>
#include<stack>
using namespace std;
typedef struct node
{
int x;
int y;
int c;//表示方向
}Node;
//int dir[4][2] = { 1, 0, 0, -1, -1, 0, 0, -1 }; //顺时针方向 右 下 左 上
int dir[4][2] = { 0,1 , 1,0 ,0,-1 , -1,0 }; //顺时针方向 右 下 左 上
int matrix[10][10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,//0
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 0, 1, 1, 1, 0, 1, 0,
0, 1, 1, 1, 1, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 1, 1, 1, 1, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 0,//5
0, 1, 0, 1, 1, 1, 0, 1, 1, 0,
0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,//9
// 0 1 2 3 4 5 6 7 8 9
//0表示不通 1表示通
};
//by zhaoyang 2014.4.16
int main()
{
Node S,e,c;
S.x = 1;
S.y = 1;
S.c = -1;
e.x = 8; e.y = 8;
stack<Node> A;
A.push(S);
int flag = 0;
while (!A.empty())
{
c = A.top();
//A.pop();
if (c.c == 3 || c.c==4)
{
//matrix[c.x][c.y] = 0;
A.pop();
continue;
}
while (c.c < 4)
{
c.c++;
A.top().c = c.c;
if (c.x + dir[c.c][0] == e.x && c.y + dir[c.c][1] == e.y) { flag = 1; cout << "我擦 ,找到出口了1!!"; }
if (matrix[c.x + dir[c.c][0]][c.y + dir[c.c][1]] == 1){
//A.push(c);
matrix[c.x + dir[c.c][0]][c.y + dir[c.c][1]] = 0;
c.x = c.x + dir[c.c][0]; c.y = c.y + dir[c.c][1]; c.c = -1;
A.push(c);
break;
}
}
if (flag) break;
}
if (A.empty()) cout << "没有找到出口" << endl;
else
{
cout << "成功找到出口!" << endl;
cout << "逆序输出路径:" << endl;
while (!A.empty())
{
Node t = A.top();
A.pop();
cout << "(" << t.x << "," << t.y << ") ";
}
}
return 0;
}
//心酸的结尾: 为什么我对眼里常含泪水 因为我对这程序 爱的 深沉。。。