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, he beloved. However, it's terribly tough for a pig to go out of the the Pigpen because the pigpen are divided into M * N grids with fences Which pigs cannot go across. Luckily, there is some doors unlocked on the fences so this sunnypig can push them open with his nose. Since Sunnypig was a pig, no matter how clever he was, he can never walk upright like human beings. As a result, Sunnypig is not the able to pulling any doors.
Now give your map of the pigpen, please calculate the fewest number of doors sunnypig should push to go out of the PIGP En.
Input
The first line there is a number T (0 < T < b), denoting the number of the the test case.
The first line of all test case had only a numbers:m, N.
The following 2*m+1 lines describe the pigpen. Each line has 2*n+1 characters. ' * ' represents a cross point of 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 a 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 off 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-line.
Sample Input
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
Test instructions: A pig to go out, at least to open a few doors, to pay attention to the different doors can only open in one direction, and the exit of the place, there must be a door thinking: compare the water of the BFS
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue& Gt #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace Std; #define LS 2*i #define RS 2*i+1 #define UP (i,x,y) for (i=x;i<=y;i++) #define OFF (i,x,y) for (i=x;i>=y;i--) #define Me M (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<node> 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; }
Csu1604:sunnypig (BFS)