程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> CSU1604: SunnyPig(BFS)

CSU1604: SunnyPig(BFS)

編輯:關於C++

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

HINT

 

Source


題意:一只豬要出去,最少要開幾扇門,要注意對於不同的門只能朝一個方向打開,而出口的地方,必然有一扇門 思路:比較水的BFS
#include  
#include  
#include  
#include  
#include  
#include 
#include  
#include  
#include  
#include  
using namespace std; 
#define ls 2*i 
#define rs 2*i+1 
#define up(i,x,y) for(i=x;i<=y;i++) 
#define down(i,x,y) for(i=x;i>=y;i--) 
#define mem(a,x) memset(a,x,sizeof(a)) 
#define w(a) while(a) 
#define LL long long 
const double pi = acos(-1.0); 
#define Len 200005 
#define mod 19999997 
const int INF = 0x3f3f3f3f; 
#define exp 1e-8 
  
struct node 
{ 
    int x,y,door; 
}; 
  
int mat[1000][1000],n,m,t,vis[1000][1000],sx,sy; 
char str[1005]; 
int to[5][2]= {0,0,0,1,1,0,0,-1,-1,0}; 
  
bool outdoor(int x,int y) 
{ 
    if(x==0||y==0||x==n-1||y==m-1) 
        return true; 
    return false; 
} 
  
bool check(int x,int y) 
{ 
    if(x<0||y<0||x==n||y==m) 
        return true; 
    if(mat[x][y]==-1||vis[x][y]) 
        return true; 
    return false; 
} 
  
void bfs() 
{ 
    queue Q; 
    node a,next; 
    int i,j; 
    a.x = sx; 
    a.y = sy; 
    a.door = 0; 
    vis[sx][sy] = 1; 
    Q.push(a); 
    while(!Q.empty()) 
    { 
        a = Q.front(); 
        Q.pop(); 
        if(outdoor(a.x,a.y)&&mat[a.x][a.y]>0) 
        { 
            printf("%d\n",a.door); 
            return ; 
        } 
        up(i,1,4) 
        { 
            next = a; 
            next.x+=to[i][0]; 
            next.y+=to[i][1]; 
            if(check(next.x,next.y)) continue; 
            if(mat[next.x][next.y]>0 && mat[next.x][next.y]!=i) continue; 
            if(mat[next.x][next.y]>0) 
            { 
                next.door++; 
                if(outdoor(next.x,next.y)) 
                { 
                    printf("%d\n",next.door); 
                    return ; 
                } 
            } 
            vis[next.x][next.y] = 1; 
            Q.push(next); 
        } 
    } 
    printf("-1\n"); 
} 
  
int main() 
{ 
    int i,j,k; 
    scanf("%d",&t); 
    w(t--) 
    { 
        scanf("%d%d\n",&n,&m); 
        n=2*n+1; 
        m=2*m+1; 
        mem(mat,-1); 
        up(i,0,n-1) 
        { 
            gets(str); 
            up(j,0,m-1) 
            { 
                if(str[j]==' '||str[j]=='*') 
                    mat[i][j]=0; 
                else if(str[j]=='-'||str[j]=='|') 
                    mat[i][j]=-1; 
                else if(str[j]=='E') 
                    mat[i][j]=1; 
                else if(str[j]=='S') 
                    mat[i][j]=2; 
                else if(str[j]=='W') 
                    mat[i][j]=3; 
                else if(str[j]=='N') 
                    mat[i][j]=4; 
                else if(str[j]=='O') 
                { 
                    sx = i; 
                    sy = j; 
                    mat[i][j]=0; 
                } 
            } 
        } 
        bfs(); 
    } 
  
    return 0; 
} 




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved