码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3626 Mud Puddles

时间:2014-07-29 17:47:12      阅读:247      评论:0      收藏:0      [点我收藏+]

标签: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 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 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;
}


POJ 3626 Mud Puddles,布布扣,bubuko.com

POJ 3626 Mud Puddles

标签:des   style   color   os   io   for   art   div   

原文地址:http://blog.csdn.net/u013582254/article/details/38273899

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!