【模板】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的更多相关文章

  1. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

  2. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  3. TwoSAT算法模板

    该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真”  等价于 “Xi为假 –& ...

  4. 马的遍历(BFS

    https://www.luogu.org/problemnew/show/P1443 模板BFS...... #include<iostream> #include<cstdio& ...

  5. [原]poj2243-Knight Moves-水bfs

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  6. Pots POJ 3414

    /* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...

  7. 题解-CF677D Vanya and Treasure

    CF677D Vanya and Treasure 有一个 \(n\times m\) 的矩阵 \(a(1\le a_{i,j}\le p)\),求从起点 \((1,1)\) 出发依次遍历值为 \(1 ...

  8. PAT1076. Forwards on Weibo(标准bfs模板)

    //标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 # ...

  9. BFS 模板

    转自:欣哥 下面是bfs一般的形式,谈不上模板但一般都这么来做有点乱有什么多交流 bfs一般用于求最短时间 #include<stdio.h>#include<queue>us ...

随机推荐

  1. Maven_pom.xml介绍

    Maven的pom.xml介绍 6.1     简介 pom.xml文件是Maven进行工作的主要配置文件.在这个文件中我们可以配置Maven项目的groupId.artifactId和version ...

  2. Node.js的高性能封装 Express.js

    Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用.Express 不对 node.js 已有的特性进行二次抽象,我们只是在它之上扩展了W ...

  3. 用atom写LaTeX文档

    下载并安装Tex Live: 下载页面 下载并安装atom:下载页面 打开atom File -> Settings -> Install 搜索并安装: language-latex la ...

  4. js写当鼠标悬浮及移开出现背景变化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. css小记(3)

    1.只有块级元素可以设置背景图,行内元素要变成块级元素才可以:2.盒子不设置宽度,只设置高度,默认为100%,并自适应屏幕大小变化,为这个盒子设置margin可以在保证margin效果的同时默认调整盒 ...

  6. phpwind < v6 版本命令执行漏洞

    phpwind/sort.php 会定期每天处理一次帖子的浏览量.回复量.精华版排序 代码直接使用savearray将数据库查询出来的内容写入php文件,savearray出来的参数,都使用" ...

  7. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  8. Excel导出公共函数

    /// <summary> /// 将一组对象导出成EXCEL /// </summary> /// <typeparam name="T">要 ...

  9. DateUtils

    package com.vcredit.ddcash.batch.util; import java.text.SimpleDateFormat;import java.util.Calendar;i ...

  10. js中特有语句-with

    <script type="text/javascript"> /* *为了简化对象调用内容的书写. *可以用js中的特有语句with来完成. *格式 *with{ * ...