广度优先搜索的学习
来源:互联网 发布:软件研发过程模型 编辑:程序博客网 时间:2024/05/29 12:30
同样也是用一个迷宫的例子
#include<iostream>using namespace std;struct note{int x;//横坐标int y;//纵坐标int s;//步数};int main() {struct note que[2501];//设地图50X50,所以队列2500int a[51][51] = { 0 };//地图int book[51][51] = { 0 };//标记哪些点走过了int next[4][2] = {{0,1},//右{1,0},//下{0,-1},//左{-1,0}//上};int head, tail,flag;int next_x, next_y;int n,m,start_x,start_y,p,q;cout << "输入行和列" << endl;cin >> n >> m;cout << "输入迷宫" << endl;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> a[i][j];cout << "输入起点坐标" << endl;cin >> start_x >> start_y;cout << "输入终点坐标" << endl;cin >> p >> q;head = 1;//队列初始化tail = 1;que[tail].x = start_x;que[tail].y = start_y;que[tail].s = 0;tail++;book[start_x][start_y] = 1;flag = 0;//标记是否到达,1为到达while (head<tail){for (int i = 0; i < 4; i++) {next_x = que[head].x + next[i][0];//顺时针方向,上下左右next_y = que[head].y + next[i][1];if (next_x<1 || next_x>n || next_y<1 || next_y>m)//判断越界continue;if (a[next_x][next_y] == 0 && book[next_x][next_y] == 0) {//判断是否可走book[next_x][next_y] = 1;//宽度遍历只要入队一次,所以不需要将book还原que[tail].x = next_x;que[tail].y = next_y;que[tail].s = que[head].s + 1;tail++;}if (next_x == p&&next_y == q) {//判断到达flag = 1;break;}}if (flag == 1)break;head++;//当一个点扩展结束后,head++才能对下一个点扩展}cout << que[tail - 1].s << endl;//tail是指向队尾的下一个位置,所以要减一return 0;}
1 0
- 广度优先搜索的学习
- 广度优先搜索(bfs)的学习
- 广度优先搜索的思想
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 广度优先搜索的实现
- 广度优先搜索的代码
- 图的 广度优先 搜索
- 图的广度优先搜索
- 邻接矩阵的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 广度优先搜索的实现
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Android之 Animations动画
- 级联菜单(2个tableView共用一个数据源的方式)
- 用file_get_contents抓取网页乱码的2种解决方式
- poj 2886 Who Gets the Most Candies?(线段树+反素数)
- 广度优先搜索的学习
- 业务中多个人工作间的协作思考
- 103. Binary Tree Zigzag Level Order Traversal
- 258. Add Digits LeetCode
- Hibernate配置文件详解
- Codeforces 618 E . Robot Arm
- 104. Maximum Depth of Binary Tree LeetCode
- 【BZOJ2759】一道动态树的好题
- Maven 项目无法在Ecplise加进tomcat server