POJ 3626 Mud Puddles

来源:互联网 发布:淘宝直通车与淘宝联盟 编辑:程序博客网 时间:2024/05/01 11:50

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,搞得。。开始以为1000*1000的点暴搜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;}


1 0
原创粉丝点击