#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 ;
}

NO3——BFS的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. jquery mobile 移动web(1)

    轻量级框架jQuery Mobile 所需文件 <link rel="stylesheet" href="jquery.mobile-1.1.2/jquery.mo ...

  2. 【TOJ 3369】CD(二分)

    描述 Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. ...

  3. c++cmb

    #include<windows.h> #include<bits/stdc++.h> using namespace std; ]; int main() { printf( ...

  4. 洛谷P1709 [USACO5.5]隐藏口令Hidden Password(最小表示法)

    题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...

  5. poj_3256_Cow Picnic

    The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ ...

  6. Mac上从gitlab上拉项目实战总结

    建立公钥,私钥 https://blog.csdn.net/jigongdajiang/article/details/65441923 2019-01-03 比较喜欢使用图形化界面

  7. dom技术解析xml (php)

    1.xml实例 test.xml <?xml version="1.0" encoding="utf-8"?><!DOCTYPE 班级 SYS ...

  8. 【php】php 生僻知识点认知

    资料引用来源:http://www.runoob.com/php/php-tutorial.html ​ 表单提交中, 变量名中的点和空格被转换成下划线.例如 <input name=" ...

  9. JS 红包随机

    微信随机红包,指定金额指定用户,随机发送红包 var moneys = new Array(); var moneyTotal = 0; function rand(obj){ if(obj.size ...

  10. redis搭建

    redis 1.简介.安装 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像 ...