6.基于STL STACK的 迷宫算法

// 这是 一个心酸的开头语  用栈来尝试写迷宫算法是一种勇气  放着好好的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;

}


//心酸的结尾:  为什么我对眼里常含泪水  因为我对这程序 爱的 深沉。。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值