Finding Nemo (POJ 2049 Super egg-sore BFS)

Source: Internet
Author: User

Language:DefaultFinding Nemo
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 8372 Accepted: 1944

Description

Nemo is a naughty boy. One day he went to the deep sea all by himself. Unfortunately, he became lost and couldn ' t find his "home. Therefore, he sent a signal to him father, Marlin, to-ask for help.
After checking the map, Marlin found that the sea was like a labyrinth with walls and doors. All the walls is parallel to the x-axis or to the y-axis. The thickness of the walls is assumed to be zero.
All the doors is opened on the walls and has a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there could be some virulent medusas near the doors), Marlin wants to go through As few doors as he could to find Nemo.
Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.

We assume Marlin ' s initial position are at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum numbe R of Doors Marlin have to go through in order to reach Nemo.

Input

The input consists of several test cases. Each test case was started by a non-negative integers m and N. m represents the number of walls in the Labyrinth and N re Presents the number of doors.
Then follow M lines, each containing four integers this describe a wall in the following format:
X y D t
(x, y) indicates the lower-left point of the wall, D is the direction of the wall--0 means it's parallel to the x-axis a nd 1 means that it's parallel to the y-axis, and T gives the length of the wall.
The coordinates of ends of any wall would be in the range of [1,199].
Then there is N lines that give the description of the Doors:
X y D
X, Y, D has the same meaning as the walls. As the doors has a fixed length of 1, T is omitted.
The last line of all case contains, positive float numbers:
F1 F2
(f1, F2) gives the position of Nemo. And it would not be lie within any wall or door.
A test Case of M =-1 and N =-1 Indicates the end of input, and should not being processed.

Output

For all test case, with a separate line, please output the minimum number of doors Marlin have to go through in order to Res Cue his son. If He can ' t reach Nemo, output-1.

Sample Input

8 91 1 1 32 1 1 33 1 1 34 1 1 31 1 0 31 2 0 31 3 0 31 4 0 32 1 12 2 12 3 13 1 13 2 13 3 11 2 03 3 04 3 11.5 1.54 01 1 0 11 1 1 12 1 1 11 2 0 11.5 1.7-1-1

Sample Output

5-1

Source

Beijing 2004



Test instructions: Two-bit coordinates to tell some walls and doors, son trapped inside, father in (0,0) set out to save son, required to pass through the minimum number of doors, the minimum number of gates.

Idea: I turned it into a normal two-dimensional map, first from (0,0) Dfs traveled all the points outside the maze and marked, and then from the location of the son Bfs,step record through the number of doors, when the walk to the maze outside the DFS visited points to show out. Today do a few poj on the question how all so egg ache, really no language, the actual test data has more than 199!!! specific to see discuss bar, I also looked inside the only before ...

Code:

#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi  ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b;  i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d         ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1000;co NST int MAXN = 2005;const int MAXM = 200010;struct node{int x,y,step;    BOOL operator< (const Node &a) Const {return step>a.step; }};int n,m;int n,m,sx,sy;int dir[4][2]={1,0,0,1,-1,0,0,-1};int mp[maxn][maxn];bool vis[maxn][maxn];int door[maxn][    Maxn];bool isok (int x,int y) {if (x>=0&&x<=n&&y>=0&&y<=m) return true; return false;}    void Dfs (int x,int y) {mp[x][y]=2;        for (int i=0;i<4;i++) {int dx=x+dir[i][0];        int dy=y+dir[i][1];    if (isOK (Dx,dy) &&mp[dx][dy]==0&&!door[dx][dy]) DFS (DX,DY);    }}int BFs () {Node st,now;    memset (vis,false,sizeof (VIS));    St.x=sx,st.y=sy,st.step=0;    Vis[sx][sy]=true;    priority_queue<node>q;    Q.push (ST); while (! Q.empty ()) {st=q.top ();        Q.pop ();        if (mp[st.x][st.y]==2) return st.step;            for (int i=0;i<4;i++) {now.x=st.x+dir[i][0];            NOW.Y=ST.Y+DIR[I][1]; if (isOK (now.x,nOW.Y) (&&mp[now.x][now.y]!=1&&!vis[now.x][now.y]) {now.step=st.step;                if (door[now.x][now.y]) now.step++;                Vis[now.x][now.y]=true;            Q.push (now); }}} return-1;}    int main () {//#ifndef online_judge//freopen ("C:/users/lyf/desktop/in.txt", "R", stdin);//#endif int i,j,x,y,d,l;    Double xx,yy;        while (scanf ("%d%d", &m,&n)) {if (n==-1&&m==-1) break;        memset (mp,0,sizeof (VIS));        memset (door,0,sizeof (door));        n=m=0;            for (i=0;i<m;i++) {scanf ("%d%d%d%d", &x,&y,&d,&l);                if (d==1) {N=max (n,2*x);                M=max (m,2* (y+l));            For (j=2*y;j<=2* (y+l); j + +) Mp[2*x][j]=1;                } else {N=max (n,2* (x+l));                M=max (M,2*y);             For (j=2*x;j<=2* (x+l); j + +)       Mp[j][2*y]=1;            }} for (i=0;i<n;i++) {scanf ("%d%d%d", &x,&y,&d);                if (d==1) {mp[2*x][2*y+1]=0;            Door[2*x][2*y+1]=1;                } else {mp[2*x+1][2*y]=0;            Door[2*x+1][2*y]=1; }} n++;        m++;        cin>>xx>>yy; if (n==0 && m==0) printf ("0\n"); else if (xx<0| | yy<0| | xx>199| |            yy>199) printf ("0\n"), Else {sx= ((int) xx) *2+1;            sy= ((int) yy) *2+1;            DFS (a);        printf ("%d\n", BFS ()); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Finding Nemo (POJ 2049 Super egg-sore 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.