POJ 3626 Mud Puddles

來源:互聯網
上載者:User

標籤:des   style   color   os   io   for   art   div   

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 at (XY) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (AiBi) (-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: XY, and N.
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

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

就一BFS,搞得。。開始以為10000的點暴搜TLE。

結果=。=,都加了500,弄到第一象限去了。。

#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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.