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. ROS的文件系统 (二)

    ROS的文件系统的基本概念, ROS文件系统中的两个最基本的概念:Package和Manifest,即包和清单文件. Package是组织ROS代码的最基本单位,每一个Package都可以包括库文件, ...

  2. 线程系列4---sleep()和wait()方法区别

    2013-12-25 14:49:00 1. sleep()方法是Thread类的一个静态方法,可以在任意地方被调用,而wait()方法是object类的一个方法,只能在同步代码块或者同步方法里面,通 ...

  3. 神奇的NOIP模拟赛 T1 LGTB 玩扫雷

    LGTB 玩扫雷 在一个n m 的棋盘上,有位置上有雷(用“*” 表示),其他位置是空地(用“.” 表示).LGTB 想在每个空地上写下它周围8 个方向相邻的格子中有几个雷.请帮助他输出写了之后的棋盘 ...

  4. TClientDataSet 设计期 多次New 字段问题

    第一次New几个字段后,右键菜单CreateDataSet 后来需要再New几个字段. 右键菜单,先 ClearData(不这样,会报 打开的数据集不能执行 这个New字段的操作),然后在 字段编辑器 ...

  5. arm裸机驱动错误总结

    错误001:

  6. 创建dialog

    创建一个dialog有一下两种方式: 1.Data属性:DOM添加属性data-toggle="dialog"后,单机触发. a链接打开: <a href="jso ...

  7. 浅谈Ddos攻击攻击与防御

    EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-2-10From: http://www.80sec.com/ [ 目录 ]一 ...

  8. detangle c++ symbols

    hust$ c++filt  _ZN1AC2Ev hust$A::A()

  9. RAID-4与模2和

    在网络传输和磁盘数据管理中经常涉及到的所谓奇偶校验:每N个bit之后加上一个bit保证这N + 1bit的模2和为0(也叫异或,一个意思) 而如果这其中出现了单bit错, 直接导致校验出差,出现偶数b ...

  10. golang函数调用计时

    package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...