CSU1604 sunnypig

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

<span class="sampledata">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*</span>

Sample Output

<span class="sampledata">2
</span><p><span class="sampledata">-1</span></p><p><span class="sampledata">第一次跟學長們打比賽,很輕鬆地被虐了。除了最後兩道水題,這一道是我覺得最簡單的,dfs和bfs都用了,可是錯了八次還是沒過,╮(╯▽╰)╭。。。第二天再看了別人的解題報告,原來是漏了一個條件:一定是從門出去的。。。</span></p><p>下面是代碼</p><p><pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
#define maxN 2000+5
int n,m,p[maxN][maxN],d[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}},sx,sy;
int a[maxN][maxN];
struct node
{
    int x,y,ans;
};
int jud(int x,int y)
{
    if(!p[x][y]&&a[x][y]!=-1&&x>0&&y>0&&x<=n&&y<=m)
        return 1;
    return 0;
}
int out(int x,int y)
{
    if(x==0||y==0||x==n+1||y==m+1)
        return 1;
    return 0;
}
void bfs()
{
    queue<node> q;
    node cur,next;
    memset(p,0,sizeof(p));
    p[sx][sy]=1;
    cur.x=sx,cur.y=sy,cur.ans=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        //printf("%d %d\n",cur.x,cur.y);
        for(int i=1;i<=4;i++)
        {
            int dx=cur.x+d[i][0];
            int dy=cur.y+d[i][1];
            if(jud(dx,dy))
            {
                next.x=dx,next.y=dy;
                if(a[dx][dy]==i)
                {
                    if(out(dx+d[i][0],dy+d[i][1]))
                    {
                        printf("%d\n",cur.ans+1);
                        return ;
                    }
                    next.ans=cur.ans+1;
                    p[next.x][next.y]=1;
                    q.push(next);
                }
                else if(a[dx][dy]==0)
                {
                    next.ans=cur.ans;
                    p[next.x][next.y]=1;
                    q.push(next);
                }
            }
        }
    }
    printf("-1\n");
}
int main()
{
    int t,i,j;
    char str[maxN];
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d%d\n",&n,&m);
        n=2*n+1,m=2*m+1;
        for(i=1;i<=n;i++)
        {
            gets(str+1);
            for(j=1;j<=m;j++)
            {
                if(str[j]=='*'||str[j]=='|'||str[j]=='-')
                    a[i][j]=-1;
                if(str[j]=='E')
                    a[i][j]=1;
                if(str[j]=='W')
                    a[i][j]=2;
                if(str[j]=='S')
                    a[i][j]=3;
                if(str[j]=='N')
                    a[i][j]=4;
                if(str[j]==' ')
                    a[i][j]=0;
                if(str[j]=='O')
                {
                    sx=i,sy=j;
                    a[i][j]=0;
                }
            }
        }
        bfs();
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章