模板 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的更多相关文章
- POJ 3278 Catch That Cow(模板——BFS)
题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- TwoSAT算法模板
该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真” 等价于 “Xi为假 –& ...
- 马的遍历(BFS
https://www.luogu.org/problemnew/show/P1443 模板BFS...... #include<iostream> #include<cstdio& ...
- [原]poj2243-Knight Moves-水bfs
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- Pots POJ 3414
/* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...
- 题解-CF677D Vanya and Treasure
CF677D Vanya and Treasure 有一个 \(n\times m\) 的矩阵 \(a(1\le a_{i,j}\le p)\),求从起点 \((1,1)\) 出发依次遍历值为 \(1 ...
- PAT1076. Forwards on Weibo(标准bfs模板)
//标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 # ...
- BFS 模板
转自:欣哥 下面是bfs一般的形式,谈不上模板但一般都这么来做有点乱有什么多交流 bfs一般用于求最短时间 #include<stdio.h>#include<queue>us ...
随机推荐
- DropDownList 选中change
<script language="javascript" type="text/javascript">$(function(){ $(&q ...
- butterknife简介及Generate ButterKnife Injections 不出现的问题解决
一.概述 butterknife是一款as的功能强大插件.有了它,你几乎可以和findViewById说byebye了. 二.使用 github地址:https://github.com/avast/ ...
- intellij idea 插件 ideaVim
像Eclipse一样,idea这个公认最好的javaIDE也有Vim插件. 安装方法 File>Settings>Plugins>Install JetBrains plugin.. ...
- Hbuider 同步github
别人的教程,仅作收藏. http://blog.csdn.net/u011871921/article/details/44238971
- wampserver的php.ini文件
在修改php.ini文件时,找到了php文件夹下的php.ini文件,但是重启所有服务后就是不起作用.查看前辈的博客后,明白了是在apache目录下的php.ini才是起作用的. .
- java四种内部类详解
一般来说,有4中内部类:常规内部类.静态内部类.局部内部类.匿名内部类. 一.常规内部类:常规内部类没有用static修饰且定义在在外部类类体中. 1.常规内部类中的方法可以直接使用外部类的实例变 ...
- WordPress的body_class()函数详解
wordpress的body_class()函数,顾名思义,这个函数根据不同的页面类型为body标签生成class选择器,从而让设计人员可以各方便灵活的控制不同页面中的各个元素.本文对这一函数进行了详 ...
- Mongodb数据库加密存储(python)
需求: 不知道大家有没有遇到过这样的需求:自己的服务器出于对数据库安全的保护,需要对存储的数据进行加密保护.这样万一数据库被人拿到,别人也不能拿到数据库里面的内容.这里还有一个前提:前端的展示页面是 ...
- JavaBean组件的基本使用-语法
<jsp:useBean id="实例化对象名称" scope="保存范围" class="包.类名"> </jsp:us ...
- what's the difference between dim as and dim as new?
what's the difference between dim as and dim as new? There is no difference with value types (Intege ...