码迷,mamicode.com
首页 > 其他好文 > 详细

csu 1604 SunnyPig (bfs)

时间:2015-07-26 00:25:00      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

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)

标签:

原文地址:http://www.cnblogs.com/hsd-/p/4676785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!