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. hdu 4606 Occupy Cities

    http://acm.hdu.edu.cn/showproblem.php?pid=4606 两点之间如果有线段相隔的话,他们的最短路就需要经过线段的端点 把所有线段的端点也加入点数组中,求任意两个点 ...

  2. myeclipse中导入的jquery文件报错(出现红叉叉,提示语法错误)

    转自:http://blog.csdn.net/simplty/article/details/7661504

  3. Fragment在xml中但作用不是显示view

    2013-12-17 有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下: <FrameLayout xmlns:android="http://s ...

  4. Chrome 开发者工具有了设备模拟器

    今天从哥们那里学到了一个小技巧,使用chrome自带的多设备模拟器来调试页面在不同设备下的显示效果. 特地上网查了一下,记录一下. 如果想要在 Chrome 上测试网站在不同设备,不同分辨率的显示情况 ...

  5. OCR技术

    "起初我写这篇教程是在情人节,OCR可以带给你一整年的爱". 你之前肯定已经见过,OCR技术被应用于在平板电脑上将扫描文件处理成手写字迹,还被应用于谷歌最近添加到他们的Transl ...

  6. 蓝桥杯 algo_5 最短路 (bellman,SPFA)

    问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个 ...

  7. Android 移动缩放的ImageView

    今天介绍一下Android中怎么实现ImageView的缩放和移动,自定义TouchImageView. public class TouchImageView extends ImageView { ...

  8. 微软发布Windows Phone 8.1 Update 和中文版Cortana“小娜”

    Windows Phone 8.1 尚在推送进程中,它的第一个 GDR 更新就发布了.今天微软正式发布 Windows Phone 8.1 Update 和中文版 Cortana ——“小娜”. Wi ...

  9. 访问FLASH设备-W25X16

    /************************************* *文件名称:w25x16_spi.c * *功能描述:访问和写入数据到闪存w25x16 * *建立日期:2016-03-1 ...

  10. Volatile vs VolatileRead/Write?

    You should never use Thread.VolatileRead/Write(). It was a design mistake in .NET 1.1, it uses a ful ...