模板,BFS
#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;
}
模板,BFS的更多相关文章
- 模板 BFS
[模板]BFS #include <stdio.h> #include <string.h> #include <queue> using namespace st ...
- 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 ...
随机推荐
- myeclipse2014安装jad反编译插件
myeclipse上默认不能查看class文件,需要查看的话安装反编译插件 安装步骤: 准备图中框里的两个文件 1. [net.sf.jadclipse_3.3.0.jar]文件拷贝到如下路径([D: ...
- Android4.0源码Launcher启动流程分析【android源码Launcher系列一】
最近研究ICS4.0的Launcher,发现4.0和2.3有稍微点区别,但是区别不是特别大,所以我就先整理一下Launcher启动的大致流程. Launcher其实是贯彻于手机的整个系统的,时时刻刻都 ...
- WebForm 页面ajax 请求后台页面 方法
function ReturnOperation(InventoryID) { //入库 接口 if (confirm('你确认?')) { $.ajax({ type: "post&quo ...
- 解决Enter键与input 、a标签触发的事件的冲突
无论是 <button type="button" onclick="console.log('123');">123</button> ...
- unity, SkinnedMeshRenderer.bones[i]不能直接赋值
SkinnedMeshRenderer.bones[i]=xxx,这样写不报错,但也不起作用. 正确的方法是: List<Transform> boneList=new List<T ...
- MySQL错误代码大全(史上最全)
用任何主机语言调用MySQL时可能出现的错误.首先,列出了服务器错误消息.其次列出了客户端程序消息. B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: · 错误消息信息列在share/ ...
- MySQL5.0存储过程教程
Introduction 简介 MySQL 5.0 新特性教程是为需要了解5.0版本新特性的MySQL老用户而写的.简单的来说是介绍了“存储过程.触发器.视图.信息架构视图”,在此感谢译者陈朋奕的努力 ...
- atitit.spring hibernate的事务机制 spring不能保存对象的解决
atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能..log黑头马sql语言.. sessionF ...
- 词法分析器总结--flex&bison
转自:项目总结之词法分析器 无论是词法分析,还是语法分析,给我的第一感觉就是逻辑要严谨.由于项目有自己一套完整的语言和语法,设计好其对应的词法分析器和语法分析器显得尤为重要. 我们采用flex进行词法 ...
- C++之虚析构函数
代码一. #include <iostream> using namespace std; class Base { public: Base(){}; ~Base() { cout &l ...