CSU1604: SunnyPig (BFS)
Description
SunnyPig is a pig who is much cleverer than any other pig in the pigpen. one sunny morning, SunnyPig wants to go out of the pigpen to date Mrs. snail Il, 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 pig cannot go into ss. 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 shocould 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 '| 'present 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 shocould push to go out of the pigpen, in other words, the fewest number of doors SunnyPig shocould 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
23 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
Question: A pig must open at least a few doors. Note that different doors can only be opened in one direction, while the exit must have a certain idea: BFS for comparison
#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; }