BFS 模板
来源:互联网 发布:天刀捏脸数据库男性萌 编辑:程序博客网 时间:2024/06/05 09:27
#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模板
- 初识HTML(1)
- 测试时出现下载框的各种Debug
- 字符串匹配(算法导论)
- 字幕时间轴制作(2)
- 常用Maven的仓库地址
- BFS 模板
- HDU 1406 (完数)
- 【算法】任意区间寻找约数最大的数
- 数据库部分知识点
- 安卓网络编程1--ANR异常和Handler机制
- 41.Oracle数据库SQL开发之 日期和时间的存储与处理——设置默认的日期格式
- XX科技继续11-08
- Bootstrap按钮
- 42.Oracle数据库SQL开发之 日期和时间的存储与处理——Oracle对2位年份处理