csu 1604 SunnyPig (bfs)

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

				
时间: 2024-10-14 21:39:21

csu 1604 SunnyPig (bfs)的相关文章

CSU1604: SunnyPig(BFS)

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 th

BFS CSU

因为要花费最少,如果花费最少的有多个还要使得步数最少 所以在判断一个数字要不要入队列的时候只要判断这个就可以了 I - Interesting Calculator Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1336 Description There is an interesting calculator. It has 3 ro

CSU 1511: 残缺的棋盘(BFS啊 )

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 Description Input 输入包含不超过10000 组数据.每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同. Output 对于每组数据,输出测试点编号和最少步数. Sample Input 1 1 8 7 5 6 1 1 3 3 2 2 Sample

CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 1336: Interesting Calculator Description There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pressing each button appends that digit to the end of

CSU 1726 你经历过绝望吗?两次! [BFS]

Description 4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的"猪坚强".不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川"猪坚强"相比,熊本的猪可没那么幸运,因为它们最终还是没能逃过被送往屠宰场的命运. 我们假设"猪坚强"被困在一个N*M的废墟中,其中"@"表示"猪坚强"的位置,"."表示可

csu - 1566: The Maze Makers (bfs)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围一圈,用1表示墙,0表示空,这样就可以表示出一个迷宫,现在就是判断这个迷宫属于4种类型中哪种类型. 参考了 大神博客.构图很难,并且想法跟以往的题都不一样.是一个好题. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

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

1106: 最优对称路径 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 371  Solved: 77[Submit][Status][Web Board] Description 给 一个n行n列的网格,每个格子里有一个1到9的数字.你需要从左上角走到右下角,其中每一步只能往上.下.左.右四个方向之一走到相邻格子,不能斜着走, 也不能走出网格,但可以重复经过一个格子.为了美观,你经过的路径还必须关于“左下-右上”这条对角线对称.下图是一个6x6网

CSU 残缺的棋盘 (BFS)

Description Input 输入包含不超过10000 组数据.每组数据包含6个整数r1, c1, r2, c2, r3, c3 (1<=r1, c1, r2, c2, r3, c3<=8). 三个格子A, B, C保证各不相同. Output 对于每组数据,输出测试点编号和最少步数. Sample Input <span class="sampledata">1 1 8 7 5 6 1 1 3 3 2 2</span> Sample Outp

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>