日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

模板 BFS

發布時間:2023/10/11 综合教程 84 老码农
生活随笔 收集整理的這篇文章主要介紹了 模板 BFS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【模板】BFS

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; struct node
{
int x,y,step;
}; char map[][];
int vis[][];
int to[][]= {,,-,,,,,-};
int n,m,sx,sy,ex,ey,ans; int check(int x,int y)
{
if(x< || x>=n || y< || y>=m)
return ;
if(vis[x][y] || map[x][y]=='#')
return ;
return ;
} void bfs()
{
int i;
queue<node> Q;
node a,next;
a.x = sx;
a.y = sy;
a.step = ;
vis[a.x][a.y]=;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(map[a.x][a.y]=='E')
{
ans = a.step;
return ;
}
for(i = ; i<; i++)
{
next = a;
next.x+=to[i][];
next.y+=to[i][];
if(check(next.x,next.y))
continue;
next.step=a.step+;
vis[next.x][next.y] = ;
Q.push(next);
}
}
ans = -;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i,j;
for(i = ; i<n; i++)
scanf("%s",map[i]);
for(i = ; i<n; i++)
{
for(j = ; j<m; j++)
{
if(map[i][j]=='S')
{
sx = i;
sy = j;
}
}
}
memset(vis,,sizeof(vis));
bfs();
printf("%d\n",ans);
} return ;
}

總結

以上是生活随笔為你收集整理的模板 BFS的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。