POJ 3626 Mud Puddles

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

时间: 2024-08-25 06:24:27

POJ 3626 Mud Puddles的相关文章

bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #define re register 6 using namespace std; 7 #define M 502

英语电影剧本大全(中英对照)

目     录 <泰坦尼克号>全部英文剧本 TV REPORTER: Treasure hunter Brock Lovett is best known for finding Spanish gold off islands in the best Caribbean. LIZZY: It’s OK, I’ll get you in a minutes. Come on. TV REPORTER: Now he is using Russian subs to reach the most

ACM 贪心算法总结

贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (poj 2393)这里面涉及到了每周的成本,只要当前这一周的成本最低就可以了,不要去考虑后面的成本. https://www.cnblogs.com/xuxiaojin/p/9401179.html  (poj 3626) 这里面设计到奶牛毁坏花的最大成本,只要当前拖走最大成本就可以了 区间贪心问题?

poj 2226 Muddy Fields(二分图最小点覆盖)

B - Muddy Fields Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2226 Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <

【POJ 1741】Tree

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

题目链接:http://poj.org/problem?id=2226 Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The co

(最小点覆盖) poj 2226

H - Muddy Fields Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2226 Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)