Description

SunnyPig is a pig who is much cleverer than any other pigs in the pigpen. One sunny morning, SunnyPig wants to go out of the pigpen to date Mrs. Snail, his beloved. However, it’s terribly tough for a pig to go out of the pigpen because the pigpen is divided into m * n grids with fences which pigs cannot go across. Luckily, there are some doors unlocked on the fences so that SunnyPig can push them open with his nose. Since SunnyPig is a pig, no matter how clever he is, he can never walk upright like human beings. As a result, SunnyPig is not able to pull any doors. Now give you the map of the pigpen, please calculate the fewest number of doors SunnyPig should push to go out of the pigpen.

Input

The first line there is a number T (0 < T < 100), denoting the number of the test case. The first line of each test case has only two numbers: m, n. The following 2*m+1 lines describe the pigpen. Each line has 2*n+1 characters. ’*’ represents a cross point of two fences. ’O’ represents the initial position SunnyPig. ’-’ and ‘|’ represent fences without doors. ’N’, ’S’, ’W’, ’E’ represent the doors SunnyPig can push in the direction of north, south, west and east respectively. And the character of a space represents the place where SunnyPig can go through.

Output

Output the fewest number of doors SunnyPig should push to go out of the pigpen, in other words, the fewest number of doors SunnyPig should push to go out of the border of these grids. If SunnyPig cannot go out of the pigpen, output -1. Each case, a single line.

Sample Input

2
3 3
*-*N*-*
|O| E E
*S*S*-*
W | E |
*-*S*N*
W W E |
*N*S*N*
4 2
*N*S*
E | W
*S*S*
EOW W
*-*N*
| W E
*-*S*
W E |
*S*S*

Sample Output

2
-1 此题是裸的bfs,重点留意 开门方向的判断 暑期训练赛3 A题 有坑点 不能用getchar;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
int m,n;
char mp[2005][2005];
int dis[4][2]= {{1,0},{-1,0},{0,-1},{0,1}};
char ch[4]={'S','N','W','E'};
struct node
{
int x;
int y;
int tim;
};
queue<node> q;
node now,ww;
int sx,sy;
int bfs(int xx,int yy)
{
while(!q.empty())
q.pop();
now.x=xx;
now.y=yy;
now.tim=0;
mp[xx][yy]='2';
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
//cout<<now.x<<now.y<<endl;
if(now.x==0||now.x==2*(m+1)||now.y==0||now.y==2*(n+1))
return now.tim;
for(int i=0; i<=3; i++)
{
//cout<<"****"<<endl;
int s1=now.x+dis[i][0];
int s2=now.y+dis[i][1];
if(s1>=0&&s1<=2*(m+1)&&s2>=0&&s2<=2*(n+1)&&(mp[s1][s2]=='1'||mp[s1][s2]==' '||mp[s1][s2]==ch[i]))
{ if(mp[s1][s2]==ch[i])
ww.tim=now.tim+1;
else
ww.tim=now.tim;
ww.x=s1;
ww.y=s2;
mp[s1][s2]='2';
q.push(ww);
}
} }
return -1;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
for(int i=1; i<=t; i++)
{
memset(mp,0,sizeof(mp));
scanf("%d%d",&m,&n);
for(int j=0; j<=2*m+2; j++)
{
for(int k=0; k<=2*n+2; k++)
{
if(j==0||j==2*(m+1)||k==0||k==2*(n+1))
mp[j][k]='1';
else
{
scanf("%c",&mp[j][k]);
if(mp[j][k]=='O')
{
sx=j;
sy=k;
}
}
}
char ke[2];
if(j!=2*m+2)
gets(ke);//用getchar 就wa 有坑点
}
cout<<bfs(sx,sy)<<endl; }
}
return 0;
}

												

csu 1604 SunnyPig (bfs)的更多相关文章

  1. csu 1356 Catch bfs(vector)

    1356: Catch Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 40[Submit][Status][Web Board] ...

  2. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  3. CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1726 1726: 你经历过绝望吗?两次! Submit Page    Summar ...

  4. CSU 1259 bfs找最短路

    题目大意: 不想介绍,题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1259 bfs求最短路. 这里因为2-9,到达同样的点不计步数,那我 ...

  5. csu 最优对称路径(bfs+记忆化搜索)

    1106: 最优对称路径 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 371  Solved: 77[Submit][Status][Web Boar ...

  6. csu - 1566: The Maze Makers (bfs)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围 ...

  7. UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)

    题意: 3*3方格,有一个是空的.其他的每个格子里有一个立方体.立方体最初上下白色,前后红色,左右蓝色.移动的方式为滚.给出初态空的位置,终态上面颜色情况,问最少多少步能到达.如果超过30步不能到达, ...

  8. CSU-ACM2018暑假集训6—BFS

    可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...

  9. CSU - 1224 ACM小组的古怪象棋

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1224 1224: ACM小组的古怪象棋 Lime Limit: 1 Sec     ...

随机推荐

  1. 微软WTL模板库完整版安装(VS2010+windows7X64位环境下)分享

    一:简介 想必大家对于微软的MFC应该都比较熟悉.但是WTL可能很多人比较陌生吧.下面我就简单的说说这个库. 首先对这个库的做个简单的介绍吧. WTL 是 Windows Template Libra ...

  2. GCJ 2015-Qualification-C Dijkstra 特殊注意,展开 难度:2

    https://code.google.com/codejam/contest/6224486/dashboard#s=p2 题目中的新运算满足传递性不满足自反性,满足传递性则可以先计算后面的部分再计 ...

  3. <input type="hidden" id="haha" name="wang" value="xiaodong" />

    jsp中一个隐藏的文本框,文本框里的值是:xiaodong id属性和name属性:就是在JavaScript中或者控制器中根据id或name属性取它的value的值 开发人员所需要,又不想让用户看到 ...

  4. svm特征

    svm特征格式:<label><index1>:<value1><index1>:<value1>.... 其中<label> ...

  5. Win7 Print Spooler服務自动关闭

    对于Win7系统而言,该问题通常是安装了错误的打印驱动引起的,Win7系统为了保护其它进程不受干扰,自动关闭了打印服务. 解决方法就是: a> 把不用的打印机删掉. b> 确保你安装了正确 ...

  6. jQuery 1.7_20120209 学习笔记

    html([val|fn]) parameters: function(index,html) 此函数返回一个html字符串,接受两个参数,index为元素在集合中的索引位置,html为原先的html ...

  7. jsCodeWar 多函数嵌套调用

    function compose(f, g) { return function() { return f(g.apply(this, arguments)); }; } --- function c ...

  8. bistu新生-1004

    #include "stdio.h"#include "stdlib.h"#include "math.h"int cmp(const vo ...

  9. C++11 move_iterator

    template<typename Iterator> class move_iterator { Iterator current; public: typedef Iterator i ...

  10. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...