广度优先搜索(Breadth First Search,BFS)
来源:互联网 发布:红鸟棋牌游戏源码 编辑:程序博客网 时间:2024/05/22 00:28
广度优先搜索
思想:以一个未被访问的结点作为起始结点,访问其所有相邻结点,然后对每一个相邻的结点,再访问它们相邻的未被访问的结点。
实现:使用队列从起始结点到子结点扩展,在遍历图中相当于前序遍历
Sample In
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3
Sample Out
7
//C代码#include <cstdio>#include <cstring>#include <iostream>using namespace std;struct note{ int x; int y; int s;//步数};int main(){ struct note que[2501];//队列 int head,tail;//队列头尾指针 int i,j,k,n,m,startx,starty,p,q,tx,ty,flag; int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int a[51][51]={0}; int book[51][51]={0}; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&a[i][j]); scanf("%d %d %d %d",&startx,&starty,&p,&q); //队列初始化 head =1; tail =1; que[tail].x=startx; que[tail].y=starty; que[tail].s=0; tail++; book[startx][starty]=1; flag=0; while(head<tail){ for(k=0;k<=3;k++){ tx=que[head].x+next[k][0]; ty=que[head].y+next[k][1]; //判断是否越界 if(tx<1||tx>n||ty<1||ty>m) continue; //判断是否是障碍物或已走过 if(a[tx][ty]==0&&book[tx][ty]==0){ //加入队列 book[tx][ty] =1; que[tail].x=tx; que[tail].y=ty; que[tail].s=que[head].s+1; tail++; } if(tx==p&&ty==q){ flag=1; break; } } if(flag==1) break; head++; } printf("%d",que[tail-1].s); return 0;}
//Java代码import java.util.Scanner;public class Main { static int m,n; static int endX,endY; static int minStep = 2147483647; static int [][]next = {{0,1},{1,0},{0,-1},{-1,0}};//顺时针右下左上 static int [][]a = new int[51][51] ; static int [][]book = new int[51][51]; //模拟队列 static int[]x = new int[51] ; static int[]y = new int[51] ; static int[]s = new int[51] ; public static int bfs(int startx,int starty,int endx,int endy){ //初始化 int head =1,tail =1; x[tail] = startx; y[tail] = starty; s[tail] = 0; book[head][tail] = 1; tail++; int flag = 0; while(head<tail){ for(int k=0;k<=3;k++){ int tx = x[head] + next[k][0]; int ty = y[head] + next[k][1]; //判断是否越界 if(tx<1||tx>n||ty<1||ty>m) continue; //判断是否走过 if(book[tx][ty] == 0 && a[tx][ty] == 0){ x[tail] = tx; y[tail] = ty; s[tail] = s[head]+1; book[tx][ty] = 1; tail++; } if(tx==endx && ty==endy){ flag = 1; break; } } if(flag == 1) break; head++; } return s[tail-1]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); //地图读入 for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ a[i][j] = in.nextInt(); } } //读入起点终点 int startX = in.nextInt(); int startY = in.nextInt(); endX = in.nextInt(); endY = in.nextInt(); book[startX][startY] = 1; minStep = bfs(startX,startY,endX,endY); System.out.println("最短路径长度为:"+minStep); }}
0 0
- 广度优先搜索算法(Breadth-First-Search,BFS)
- 广度优先搜索(Breadth First Search,BFS)
- [数据结构]广度优先搜索算法(Breadth-First-Search,BFS)
- 广度优先搜索(Breadth first search)
- 广度优先搜索算法(breadth First Search, BFS)
- 图搜索算法之广度优先搜索(Breadth First Search,BFS)
- BFS——广度优先算法(Breadth First Search)
- 广度优先搜索算法(Breadth-First-Search)
- BFS-宽度优先搜索(Breadth First Search)—1
- LeetCode上Tag为广度优先搜索BFS(Breadth-first Search)的题目整理
- BFS—— Breadth First Search 广度优先算法
- 图的广度优先搜索(Breadth First Search)
- 图的遍历之广度优先搜索(Breadth First Search)
- BFS(Breadth First Search)
- 算法:图的广度优先遍历(Breadth First Search)
- Breadth First Search (BFS)入门
- 关于广度优先遍历(Breadth First Search)的提纲性内容
- 广度优先搜索算法(BFS,Broad First Search)的PHP实现
- 第三方免费加固横向对比(转)
- 关于camera be called after release()的异常
- toString()方法
- 恢复BIOS usb support
- 1034. 有理数四则运算(20)
- 广度优先搜索(Breadth First Search,BFS)
- 省份-城市-区域三级联动【Struts2 + JSON版】
- 1070. Mooncake (25)
- java向上转型和向下转型
- SpringMVC创建web项目基础之(三)——Spring MVC实现文件上传
- 几种进制数的转化
- Menu 菜单的使用
- 1035. 插入与归并(25)
- PLSQL Trigger Dynamic SQL