bfs输出路径模板

Description

定义一个二维数组: 
int maze[5][5] = {

    0, 1, 0, 0, 0,

    0, 1, 0, 1, 0,

    0, 0, 0, 0, 0,

    0, 1, 1, 1, 0,

    0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output

左上角到右下角的最短路径,格式如样例所示。
Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
代码:

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node{
	int x,y,step;
	node(){
	}
	node(int xx,int yy,int sstep):x(xx),y(yy),step(sstep){
	}
};
struct prin{
	int x,y;
}oo[1009][1009];
int book[1009][1009];
const int rr[4][2]={1,0,0,1,-1,0,0,-1};
void prin(int xx,int yy)
{
	if(xx == 1 && yy == 1)
	{
	}
	else{
		prin(oo[xx][yy].x,oo[xx][yy].y);
		printf("%d,%d\n",oo[xx][yy].x,oo[xx][yy].y);
	}
};
int a[6][6] = {
	0,0, 0, 0, 0, 0,
	0,0, 1, 0, 0, 0,
	0,0, 1, 0, 1, 0,
	0,0, 0, 0, 0, 0,
	0,0, 1, 1, 1, 0,
	0,0, 0, 0, 1, 0,
};
int tt =1;
void dfs(int x,int y,int step)
{
	queue<node>A;
	A.push(node(x,y,step));
	book[x][y] = 1;
	oo[x][y].x = x;
	oo[x][y].y = y;
	while(!A.empty())
	{
		node u =A.front();
		A.pop();
		if(u.x == 5 && u.y == 5)
		{
			prin(u.x,u.y);
			return ;
		}
		for(int i=0;i<4;++i)
		{
			int xx = u.x+rr[i][0];
			int yy = u.y+rr[i][1];
			int ss = u.step+1;
			if(xx <= 5 && yy <= 5 && a[xx][yy] == 0 && book[xx][yy] == 0&&xx>0&&yy>0)
			{
				oo[xx][yy].x = u.x;
				oo[xx][yy].y = u.y;
				A.push(node(xx,yy,ss));
				book[xx][yy] = 1;
			}
		}	
	}
	return ;
}
int main()
{
	for(int i=1;i<=5;++i)
	{
		for(int j=1;j<=5;++j)
		{
			printf("%d,",a[i][j]);
		}
		printf("\n");
	}
//	printf("0/0\n");
	dfs(1,1,0);	
	printf("5/5\n");
	return 0; 
} 
/*
01010101
00101010
10001010
00010101
01000101
01000001
01011101
00010000
*/

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是BFS和DFS算法的C++模板BFS算法模板: ```cpp #include <iostream> #include <queue> using namespace std; void BFS(int start, int end) { queue<int> q; bool visited[n]; // 标记节点是否被访问过 int level[n]; // 记录节点的层级 // 初始化visited和level数组 for (int i = 0; i < n; i++) { visited[i = false; level[i = 0; } q.push(start); visited[start = true; while (!q.empty()) { int current = q.front(); q.pop(); // 进行当前节点的操作 for (int neighbor : 获取当前节点的所有邻居节点) { if (!visited = true; level = level[current + 1; } } } // 输出结果,例如输出最短路径的长度 cout << "最短路径长度为:" << level[end << endl; } ``` DFS算法模板: ```cpp #include <iostream> using namespace std; void DFS(int current, bool visited[]) { // 进行当前节点的操作 visited[current = true; // 标记当前节点已访问 for (int neighbor : 获取当前节点的所有邻居节点) { if (!visited = false; } DFS(start, visited); return 0; } ``` 这些模板可以用于解决各种问题,如迷宫、八皇后、n皇后、油田、连通块、数独等。在使用时,根据具体问题的要求选择适合的算法,并按照注释部分进行相应的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [DFS和BFS理解+模板+例题](https://blog.csdn.net/weixin_43876357/article/details/112552683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

-lyslyslys

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

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

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

打赏作者

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

抵扣说明:

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

余额充值