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. MSMQ消息队列

    MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一 ...

  2. Achieving High Availability and Scalability - ARR and NLB

    Achieving High Availability and Scalability: Microsoft Application Request Routing (ARR) for IIS 7.0 ...

  3. Easy UI 遮罩(MASK)

    From :http://blog.csdn.net/luminji/article/details/16984839 Easy UI 的各类控件有些带了遮罩功能,如 DataGrid,可以这样使用: ...

  4. K2 如何和 Java 做整合?

    本文内容来自K2社区 问题:我们清楚K2 产品是基于.net 平台,我们有需求要将Java平台的表单和K2进行整合,使用K2.可以有什么方案建议? 专家解答: 这个需求也是比较常见的,以下是我的一些经 ...

  5. 启动BPM的5个步骤

    在大部分业务中,我们通常认为:一个主要的业务流程管理项目从设计时间开始会比较好.我们知道很多方式来提高效率,增加生产力以及简化我们员工的工 作 - 这正是业务流程管理所做的.不幸的是,不管我们意图多好 ...

  6. DotNetBar v14.0.0.3 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=14.0.0.3 如果遇到破解问题可以与我 ...

  7. typedef定义函数类型或函数指针

    转载请标明出处: 最近在看redis的代码,发现了有关函数指针的部分,想把它记下来. 在redis中有类似下面的定义,利用typedef 定义了一个新的类型,这种类型是一个函数: typedef vo ...

  8. BinaryWriter

    c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...

  9. ODI 12c 安装

    软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...

  10. Smart Card Filesystem