• 洛谷 P2873 [USACO07DEC]泥水坑Mud Puddles


    题目描述

    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 (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (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.

    清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

    输入输出格式

    输入格式:

     

    • Line 1: Three space-separate integers: X, Y, and N.

    • Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

     

    输出格式:

     

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

     

    输入输出样例

    输入样例#1: 复制
    1 2 7
    0 2
    -1 3
    3 1
    1 1
    4 2
    -1 1
    2 2
    输出样例#1: 复制
    11
    思路:宽搜
    #include<queue> 
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int sx,sy,n,flag,ans=0x7f7f7f7f;
    int tx[4]={1,-1,0,0};
    int ty[4]={0,0,1,-1};
    int map[1010][1010];
    struct nond{
        int x,y,dis;
    };
    int main(){
        scanf("%d%d%d",&sx,&sy,&n);
        sx+=500;sy+=500;
        for(int i=1;i<=n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            map[x+500][y+500]=1;
        }
        queue<nond>que;
        que.push((nond){500,500,0});
        map[500][500]=1;
        while(!que.empty()){
            nond now=que.front();
            que.pop();
            for(int i=0;i<4;i++){
                int dx=now.x+tx[i];
                int dy=now.y+ty[i];
                if(dx<0||dy<0||map[dx][dy])    continue;
                if(dx==sx&&dy==sy){ ans=now.dis+1;flag=1;break; }    
                map[dx][dy]=1;
                que.push((nond){dx,dy,now.dis+1});
            }
            if(flag)    break;
        }
        cout<<ans;
    }
     
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    C语言多文件参数传递
    第十章 C++11新特性
    第九章 STL标准库(二)
    第八章 标准模板库STL(一)
    第七章 2.泛型编程(模板)
    第七章 1.输入输出与模板
    第六章 多态
    第五章 继承与派生
    第四章 运算符重载
    第三章 类与对象进阶
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7859728.html
Copyright © 2020-2023  润新知