CSU 1604 SunnyPig (BFS)
1604: SunnyPig
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 193 Solved: 34
[Submit][Status][Web Board]
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
题意:给定一个图,起点是O,'-'和'|'分别表示篱笆,'*'表示篱笆的交点,'E','S','W','N'代表门开的方向,' '代表可以走的路,问最少经过几扇门可以出去?
分析:很水的BFS,把图改成数字表示,用1,2,3,4表示方向,-1表示不能走,0代表可以自由选择方向。注意出界的判断。
输入不用getchar吃回车用'\n'就不RE了,我也不知道是为什么。
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; const int MAXN=1000+5; int kase,n,m,sx,sy,ans; int mat[MAXN][MAXN],vis[MAXN][MAXN]; int dir[][2]={{0,0},{0,1},{1,0},{0,-1},{-1,0}}; char str[MAXN]; bool flag; struct node { int x,y; int step; }s; bool ok(int x,int y) { if(1<=x && x<=n && 1<=y && y<=m) return true; return false; } void BFS() { memset(vis,0,sizeof(vis)); queue<node> Q; s.x=sx,s.y=sy; s.step=0; vis[sx][sy]=1; Q.push(s); while(!Q.empty()) { node now,next; now=Q.front(); Q.pop(); for(int i=1;i<=4;i++) { int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; if(ok(xx,yy) && !vis[xx][yy] && mat[xx][yy]!=-1) { if(mat[xx][yy]==i) { if(!ok(xx+dir[i][0],yy+dir[i][1])) { ans=now.step+1; flag=true; return ; } next.x=xx; next.y=yy; next.step=now.step+1; vis[xx][yy]=1; Q.push(next); } else if(mat[xx][yy]==0) { next.x=xx; next.y=yy; next.step=now.step; vis[xx][yy]=1; Q.push(next); } } } } } int main() { scanf("%d",&kase); while(kase--) { memset(mat,0,sizeof(mat)); scanf("%d %d\n",&n,&m); //getchar(); n=2*n+1; m=2*m+1; for(int i=1;i<=n;i++) { gets(str+1); for(int j=1;j<=m;j++) { if(str[j]=='-' || str[j]=='|' || str[j]=='*') mat[i][j]=-1; if(str[j]==' ') mat[i][j]=0; if(str[j]=='E') mat[i][j]=1; if(str[j]=='S') mat[i][j]=2; if(str[j]=='W') mat[i][j]=3; if(str[j]=='N') mat[i][j]=4; if(str[j]=='O') { sx=i; sy=j; mat[i][j]=0; } } } flag=false; BFS(); if(flag) printf("%d\n",ans); else printf("-1\n"); } return 0; }