CSU 1604 Sunnypig (BFS)

Source: Internet
Author: User

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 have 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

The problem is a bare bfs, focusing on the direction of the open door to Judge Summer training 3 A problem has a pit point can not be used getchar;
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <stack> #include <queue> #include <vector>using namespace Std;int M,n;char mp[    2005][2005];int dis[4][2]= {{1,0},{-1,0},{0,-1},{0,1}};char ch[4]={' S ', ' N ', ' W ', ' E '};struct node{int x;    int y; int Tim;};    queue<node> q;node now,ww;int sx,sy;int bfs (int xx,int yy) {while (!q.empty ()) Q.pop ();    Now.x=xx;    Now.y=yy;    now.tim=0;    mp[xx][yy]= ' 2 ';    Q.push (now);        while (!q.empty ()) {Now=q.front ();        Q.pop ();        cout<<now.x<<now.y<<endl; if (now.x==0| | now.x==2* (m+1) | | now.y==0| |        now.y==2* (n+1)) return now.tim;            for (int i=0; i<=3; i++) {//cout<< * * * * <<endl;            int s1=now.x+dis[i][0];            int s2=now.y+dis[i][1]; if (s1>=0&&s1<=2* (m+1) &&s2>=0&&s2<=2* (n+1) && (mp[s1][s2]== ' 1' | | mp[s1][s2]== ' | |                Mp[s1][s2]==ch[i]) {if (mp[s1][s2]==ch[i]) ww.tim=now.tim+1;                else Ww.tim=now.tim;                WW.X=S1;                WW.Y=S2;                mp[s1][s2]= ' 2 ';            Q.push (WW); }}} return-1;}    int main () {int t;            while (scanf ("%d", &t)!=eof) {for (int i=1; i<=t; i++) {memset (Mp,0,sizeof (MP));            scanf ("%d%d", &m,&n);                    for (int j=0; j<=2*m+2; J + +) {for (int k=0; k<=2*n+2; k++) { if (j==0| | j==2* (m+1) | | k==0| |                    k==2* (n+1)) mp[j][k]= ' 1 ';                        else {scanf ("%c", &mp[j][k]);                            if (mp[j][k]== ' O ') {sx=j;                        Sy=k;      }                    }          } Char ke[2];        if (j!=2*m+2) gets (KE),//with GetChar on WA have pit point} cout<<bfs (Sx,sy) <<endl; }} return 0;}

CSU 1604 Sunnypig (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.