模板,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 ...
随机推荐
- centos6.5下使用yum完美搭建LNMP环境(php5.6,mysql5.5,nginx1.10)
准备工作 配置防火墙,开启80端口.3306端口 不用执行这句:rm -rf /etc/sysconfig/iptables 直接进入修改:vi /etc/sysconfig/iptables 添加8 ...
- Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now running 50641. Please use mysql_upgrade to fix this error.
出现问题: Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now runn ...
- Mysql Communications link failure 问题的解决
问题现象 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last p ...
- Linux两块4TB的数据磁盘创建8TB的Raid0
分区表MBR与GPT的说明: MBR:主引导记录,是传统的分区机制,应用于绝大多数使用BIOS的PC设备,MBR+BIOS,MBR支持32位和64位系统,支持的分区数量有限,MBR只支持不超过2T的硬 ...
- HTML十进制字符编号
有时候写文章时,段落里引用的代码莫名的就被解释到原本的html中了,通过用一些HTML里特殊字符的编码即可回到原文,保留原格式. 字符 十进制字符编号 实体名字 说明 ! ! — 惊叹号Exclama ...
- java后台分页实例一
后台框架:jfinal + velocity.前台框架:jquery 页面 <!DOCTYPE html> <html> <head> <meta char ...
- C++重载IO操作符
操作符的重载有一定的规则,而IO操作符必须重载为普通函数,且应该声明为类的友元函数.我试了,非友元也可以,但是必须提供访问成员变量的函数,所以,出于效率的考虑还是应该定义为友元. 规则如下: 1. ...
- ORACLE用户角色与授权
--创建一个用户CREATE USER test_user IDENTIFIED BY test_user; --创建一个角色 CREATE ROLE connect2 ; --为角色授权 GRANT ...
- hdu1978(记忆化搜索)
#include<iostream> #include<stdio.h> #include<string.h> #include<queue> usin ...
- js中获取event keycode的兼容办法
window.onkeypress=function(e){ var event = e || window.event, //在ff下event会做为参数传进来,ie下会在window下 keyCo ...