广度优先搜寻与深度优先搜寻代码模板

1.广度优先搜寻

#include <iostream>
#include <queue>

using namespace std;

#define MAXROW  10
#define MAXLINE  10

typedef struct _Point
{
	int _x;
	int _y;
}Point;

Point prePonit[MAXROW][MAXLINE];
int maze[MAXROW][MAXLINE] =
{
	1,1,1,1,1,1,1,1,1,1,
	0,0,0,1,1,1,1,1,1,1,
	1,1,0,1,1,1,1,1,1,1,
	1,1,0,0,0,0,1,1,1,1,
	1,1,0,1,1,0,1,1,1,1,
	1,1,0,1,1,0,1,1,1,1,
	1,1,1,1,1,0,1,1,1,1,
	1,1,1,1,1,0,0,0,1,1,
	1,1,1,1,1,1,1,0,0,0,
	1,1,1,1,1,1,1,1,1,1,
};

void displyMaze()
{
	for (int i = 0; i < MAXROW; i++)
	{
		for (int j = 0; j < MAXLINE; j++)
		{
			if (maze[i][j] == 1) printf("%2s", " *");
			else if (maze[i][j] == 2) printf("%2s", " #");
			else printf("%2s", " ");
		}
		putchar(10);
	}
	printf(" ====================\n");
}




void visit(queue<Point> &s, int x, int y, Point t)
{
	Point p = { x, y };
	s.push(p);
	prePonit[x][y] = t;
}


int main()
{
	memset(prePonit, 0xFF, sizeof(Point)*MAXROW*MAXLINE);
	displyMaze();
	Point sp = { 1, 0 }, ep = { 8, 9 };
	queue<Point> s;
	s.push(sp);

	bool flag = false;
	while (!s.empty())
	{
		Point t = s.front();
		s.pop();
		maze[t._x][t._y] = 2;
		//上
		if (t._x - 1 >= 0 && maze[t._x - 1][t._y] == 0)
			visit(s, t._x - 1, t._y, t);
		//下
		if (t._x + 1 < MAXROW && maze[t._x + 1][t._y] == 0)
			visit(s, t._x + 1, t._y, t);

		//左
		if (t._y - 1 >= 0 && maze[t._x][t._y - 1] == 0)
			visit(s, t._x, t._y - 1, t);
		//右
		if (t._y + 1 < MAXLINE && maze[t._x][t._y+1] == 0)
			visit(s, t._x, t._y + 1, t);

		if (t._x == ep._x && t._y == ep._y)
		{
			flag = true;
			break;
		}

	}
	displyMaze();
	if (flag)
	{
		cout << "Find Path!" << endl;
		Point t = ep;
		while (t._x != -1)
		{
			cout << "(" << t._x << "," << t._y << ")" << endl;
			t = prePonit[t._x][t._y];
		}
	}
	else
		cout << "Not Find Path!" << endl;


    return 0; 
}

结果:

 * * * * * * * * * *
       * * * * * * *
 * *   * * * * * * *
 * *         * * * *
 * *   * *   * * * *
 * *   * *   * * * *
 * * * * *   * * * *
 * * * * *       * *
 * * * * * * *
 * * * * * * * * * *
 ====================
 * * * * * * * * * *
 # # # * * * * * * *
 * * # * * * * * * *
 * * # # # # * * * *
 * * # * * # * * * *
 * * # * * # * * * *
 * * * * * # * * * *
 * * * * * # # # * *
 * * * * * * * # # #
 * * * * * * * * * *
 ====================
Find Path!
(8,9)
(8,8)
(8,7)
(7,7)
(7,6)
(7,5)
(6,5)
(5,5)
(4,5)
(3,5)
(3,4)
(3,3)
(3,2)
(2,2)
(1,2)
(1,1)
(1,0)

 

2.深度优先搜寻

#include <iostream>
#include <stack>

using namespace std;

#define MAXROW  10
#define MAXLINE  10

typedef struct _Point
{
	int _x;
	int _y;
}Point;

Point prePonit[MAXROW][MAXLINE];
int maze[MAXROW][MAXLINE] =
{
	1,1,1,1,1,1,1,1,1,1,
	0,0,0,1,1,1,1,1,1,1,
	1,1,0,1,1,1,1,1,1,1,
	1,1,0,0,0,0,1,1,1,1,
	1,1,0,1,1,0,1,1,1,1,
	1,1,0,1,1,0,1,1,1,1,
	1,1,1,1,1,0,1,1,1,1,
	1,1,1,1,1,0,0,0,1,1,
	1,1,1,1,1,1,1,0,0,0,
	1,1,1,1,1,1,1,1,1,1,
};

void displyMaze()
{
	for (int i = 0; i < MAXROW; i++)
	{
		for (int j = 0; j < MAXLINE; j++)
		{
			if (maze[i][j] == 1) printf("%2s", " *");
			else if (maze[i][j] == 2) printf("%2s", " #");
			else printf("%2s", " ");
		}
		putchar(10);
	}
	printf(" ====================\n");
}




void visit(stack<Point> &s, int x, int y, Point t)
{
	Point p = { x, y };
	s.push(p);
	prePonit[x][y] = t;
}


int main()
{
	memset(prePonit, 0xFF, sizeof(Point)*MAXROW*MAXLINE);
	displyMaze();
	Point sp = { 1, 0 }, ep = { 8, 9 };
	stack<Point> s;
	s.push(sp);

	bool flag = false;
	while (!s.empty())
	{
		Point t = s.top();
		s.pop();
		maze[t._x][t._y] = 2;
		//上
		if (t._x - 1 >= 0 && maze[t._x - 1][t._y] == 0)
			visit(s, t._x - 1, t._y, t);
		//下
		if (t._x + 1 < MAXROW && maze[t._x + 1][t._y] == 0)
			visit(s, t._x + 1, t._y, t);

		//左
		if (t._y - 1 >= 0 && maze[t._x][t._y - 1] == 0)
			visit(s, t._x, t._y - 1, t);
		//右
		if (t._y + 1 < MAXLINE && maze[t._x][t._y+1] == 0)
			visit(s, t._x, t._y + 1, t);

		if (t._x == ep._x && t._y == ep._y)
		{
			flag = true;
			break;
		}

	}
	displyMaze();
	if (flag)
	{
		cout << "Find Path!" << endl;
		Point t = ep;
		while (t._x != -1)
		{
			cout << "(" << t._x << "," << t._y << ")" << endl;
			t = prePonit[t._x][t._y];
		}
	}
	else
		cout << "Not Find Path!" << endl;


    return 0; 
}

结果:

 * * * * * * * * * *
       * * * * * * *
 * *   * * * * * * *
 * *         * * * *
 * *   * *   * * * *
 * *   * *   * * * *
 * * * * *   * * * *
 * * * * *       * *
 * * * * * * *
 * * * * * * * * * *
 ====================
 * * * * * * * * * *
 # # # * * * * * * *
 * * # * * * * * * *
 * * # # # # * * * *
 * *   * * # * * * *
 * *   * * # * * * *
 * * * * * # * * * *
 * * * * * # # # * *
 * * * * * * * # # #
 * * * * * * * * * *
 ====================
Find Path!
(8,9)
(8,8)
(8,7)
(7,7)
(7,6)
(7,5)
(6,5)
(5,5)
(4,5)
(3,5)
(3,4)
(3,3)
(3,2)
(2,2)
(1,2)
(1,1)
(1,0)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你看,那是海边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值