BFS模板
来源:互联网 发布:薛之谦的淘宝店uujuly 编辑:程序博客网 时间:2024/05/29 10:03
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struct node { int x,y,step; }; char map[105][105]; int vis[105][105]; int to[4][2]= {1,0,-1,0,0,1,0,-1}; int n,m,sx,sy,ex,ey,ans; int check(int x,int y) { if(x<0 || x>=n || y<0 || y>=m) return 1; if(vis[x][y] || map[x][y]=='#') return 1; return 0; } void bfs() { int i; queue<node> Q; node a,next; a.x = sx; a.y = sy; a.step = 0; vis[a.x][a.y]=1; Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(map[a.x][a.y]=='E') { ans = a.step; return ; } for(i = 0; i<4; i++) { next = a; next.x+=to[i][0]; next.y+=to[i][1]; if(check(next.x,next.y)) continue; next.step=a.step+1; vis[next.x][next.y] = 1; Q.push(next); } } ans = -1; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int i,j; for(i = 0; i<n; i++) scanf("%s",map[i]); for(i = 0; i<n; i++) { for(j = 0; j<m; j++) { if(map[i][j]=='S') { sx = i; sy = j; } } } memset(vis,0,sizeof(vis)); bfs(); printf("%d\n",ans); } return 0; }
0 0
- 【模板】BFS
- bfs模板
- BFS模板
- 模板,BFS
- BFS模板
- BFS 模板
- BFS模板
- bfs模板
- bfs模板
- BFS 模板
- BFS模板
- BFS模板
- bfs模板
- bfs模板
- bfs模板
- BFS模板
- BFS 模板
- bfs模板
- 方向梯度直方图(HOG,Histogram of Gradient)学习笔记二 HOG正篇
- 数学题--On Sum of Fractions
- putty 文件传输 pscp
- 直接型FIR滤波器的fpga实现
- ThinkPHP备忘
- BFS模板
- plsql 注册码|plsql8注册码
- 《JAVA与模式》之享元模式
- 集成个推实现推送跳转到指定页面
- Tensorflow学习笔记(8)——input_data.py解析
- 深度学习概览之自然语言处理:从基本概念到前沿研究
- Pandas学习:DataFrame
- 对fourier变换微分性质的深入理解
- iOS运行时runtime及相关Objective-C底层讲解