zoukankan      html  css  js  c++  java
  • BZOJ1627: [Usaco2007 Dec]穿越泥地

    1627: [Usaco2007 Dec]穿越泥地

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 478  Solved: 303
    [Submit][Status]

    Description

    清早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的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

    Input

    * 第1行: 3个用空格隔开的整数:X,Y 和 N

    * 第2..N+1行: 第i+1行为2个用空格隔开的整数:A_i 和 B_i

    Output

    * 第1行: 输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要 走过的最小距离

    Sample Input

    1 2 7
    0 2
    -1 3
    3 1
    1 1
    4 2
    -1 1
    2 2

    输入说明:

    贝茜所在牛棚的坐标为(1, 2)。Farmer John能看到7个泥塘,它们的坐标分
    别为(0, 2)、(-1, 3)、(3, 1)、(1, 1)、(4, 2)、(-1, 1)以及(2, 2)。
    以下为农场的简图:(*为FJ的屋子,B为贝茜呆的牛棚)

    4 . . . . . . . .
    3 . M . . . . . .
    Y 2 . . M B M . M .
    1 . M . M . M . .
    0 . . * . . . . .
    -1 . . . . . . . .
    -2-1 0 1 2 3 4 5

    X

    Sample Output

    11

    HINT

        约翰的最佳路线是:(0,0),(一1,0),(一2,0),(一2,1),(一2,2),(一2,3),(一2,4),(一1,4),(0,4),  (0,3),  (1,3),  (1,2).

    Source

    题解:
    BFS即可
    代码:
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #define inf 1000000000
    12 #define maxn 1000+100
    13 #define maxm 500+100
    14 #define ll long long
    15 using namespace std;
    16 inline ll read()
    17 {
    18     ll x=0,f=1;char ch=getchar();
    19     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    20     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    21     return x*f;
    22 }
    23 struct node
    24 {
    25     int x,y,time;
    26     node(){}
    27     node(int _x,int _y,int _time){x=_x;y=_y;time=_time;}
    28 };
    29 int dx[]={0,1,-1,0};
    30 int dy[]={1,0,0,-1};
    31 int xx0,yy0,n,v[maxn][maxn],vis[maxn][maxn];
    32 queue<node>q; 
    33 void bfs()
    34 {
    35     node now=node(500,500,0);
    36     vis[500][500]=1;
    37     q.push(now);
    38     int ans=-1;
    39     while(!q.empty())
    40     {
    41         now=q.front();q.pop();
    42         if(now.x==xx0&&now.y==yy0)
    43         {
    44             ans=now.time;
    45             break;
    46         }
    47         for(int i=0;i<4;i++)
    48         {
    49             int xx=now.x+dx[i],yy=now.y+dy[i];
    50             if(xx>=0&&yy>=0&&!v[xx][yy]&&!vis[xx][yy])
    51             {
    52                 vis[xx][yy]=1;
    53                 q.push(node(xx,yy,now.time+1));
    54             }
    55         }
    56     }
    57     printf("%d
    ",ans);
    58 }
    59 int main()
    60 {
    61     freopen("input.txt","r",stdin);
    62     freopen("output.txt","w",stdout);
    63     xx0=read(),yy0=read(),n=read();xx0+=500,yy0+=500;
    64     int x,y;
    65     while(n--)x=read(),y=read(),x+=500,y+=500,v[x][y]=1;
    66     bfs();
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    PAT甲级——A1091 Acute Stroke【30】
    PAT甲级——A1090 Highest Price in Supply Chain
    PAT甲级——A1089 Insert or Merge
    PAT甲级——A1088 Rational Arithmetic
    PAT甲级——A1087 All Roads Lead to Rome【30】
    【php中的curl】php中curl的详细解说
    【php中的curl】使用curl完成POST数据给飞信接口
    【php中的curl】php中curl的使用
    【socket】php实现socket
    【socket】用PHP的socket实现客户端到服务端的通信
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3942171.html
Copyright © 2011-2022 走看看