Poj 3626 mud puddles

Source: Internet
Author: User

Description

Farmer John is leaving his house promptly at 6 am for his daily milking of Bessie. however, the previous evening saw a heavy rain, and the fields are quite muddy. FJ starts at the point (0, 0) in the coordinate plane and heads toward Bessie who is located (X,Y) (-500 ≤X≤ 500;-500 ≤Y≤ 500). He can see allN(1 ≤N≤ 10,000) puddles of mud, located at points (AI,Bi) (-500 ≤AI≤ 500;-500 ≤Bi≤ 500) on the field. Each puddle occupies only the point it is on.

Having just bought new boots, Farmer John absolutely does not want to dirty them by stepping in a puddle, but he also wants to get to Bessie as quickly as possible. he's already late because he had to count all the puddles. if Farmer John can only travel parallel to the axes and turn at points with integer coordinates, what is the shortest distance he must travel to reach Bessie and keep his boots Clean? There will always be a path without mud that farmer John can take to reach Bessie.

Input

* Line 1: three space-separate integers:X,Y, AndN.
* Lines 2 ..N+ 1: LineI+ 1 contains two space-separated integers:AIAndBi

Output

* Line 1: The minimum distance that farmer John has to travel to reach Bessie without stepping in mud.

Sample Input

1 2 70 2-1 33 11 14 2-1 12 2

Sample output

11

It's just a BFs .. At first, we thought it was a 10000 point brute-force search for TLE.

Result =. =, They all added 500 and got the first quadrant ..

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>#include<queue>using namespace std;int mp[1100][1100];int dr[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int n1,m1,n2,m2,t;int ex,ey;struct node{    int x,y;    int step;};void bfs(){    node st,ed;    queue<node>q;    st.x=500;    st.y=500;    st.step=0;    q.push(st);    while(!q.empty())    {        st=q.front();        q.pop();        if(st.x==ex&&st.y==ey)        {            printf("%d\n",st.step);            return ;        }        for(int i=0;i<4;i++)        {            int xx=st.x+dr[i][0];            int yy=st.y+dr[i][1];            if(xx>=0&&xx<=1000&&yy>=0&&yy<=1000&&mp[xx][yy]==0)            {                ed.x=xx;                ed.y=yy;                ed.step=st.step+1;                mp[xx][yy]=1;                q.push(ed);            }        }    }}int main(){    int u,v;    while(~scanf("%d%d%d",&ex,&ey,&t))    {        ex+=500;ey+=500;        memset(mp,0,sizeof(mp));        for(int i=0;i<t;i++)        {            scanf("%d%d",&u,&v);            mp[u+500][v+500]=1;        }        bfs();    }    return 0;}


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.