zoukankan      html  css  js  c++  java
  • 穿越泥地(mud) (BFS)

    问题 C: 穿越泥地(mud)

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 16  解决: 10
    [提交][状态][讨论版]

    题目描述

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

    输入

    第1行:3个用空格隔开的整数:X,Y和N:
    第2~N+1行:第i+l行为2个用空格隔开的整数:A_i和B_i。

    输出

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

    样例输入

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

    样例输出

    11
    

    提示


    样例说明:贝茜所在牛棚的坐标为(1,2)。FJ能看到7个泥塘,它们的坐标分别为(0,2),(-1,3),(3,1),(1,1),(4,2),(-l,1),(2,2)。

    以下为农场的简图(*为FJ的屋子,B为贝茜呆的牛棚):

    y



    4| . . . . . . . .

    3| . M . . . . . .

    2| . . M B M . M .

    1| . M . M . M . .

    0| . . * . . . . .

    -1| . . . . . . . .

    --------------------→ x

    -2-1 0 1 2 3 4 5

    FJ的最佳路线是:(0,0),(-1,0),(-2,0),(-2,1),(-2,2),(-2,3),(-2,4),

    (-1,4),(0,4),(0,3),(1,3),(1,2)。

    【分析】由于某些坐标是负数,我们把每个坐标+500就成正的了。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <time.h>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define pi acos(-1.0)
    #define inf 0x3f3f3f3f
    using namespace std;
    int n,m,flag=0,a,b;
    int dis[4][2]= {1,0,0,1,-1,0,0,-1};
    int vis[1005][1005];
    int w[1005][1005];
    struct man
    {
        int x,y,step;
    };
    queue<man>q;
    void bfs(man s)
    {
        q.push(s);
    vis[s.x][s.y]=1;
        while(!q.empty())
        {
            man t=q.front();
            q.pop();
            //printf("%d %d %d
    ",t.x,t.y,t.step);system("pause");
            if(t.x==a&&t.y==b){printf("%d
    ",t.step);flag=1;return;}
           for(int i=0;i<4;i++)
           {
               int xx=t.x+dis[i][0];int yy=t.y+dis[i][1];//
               //printf("%d %d
    ",xx,yy);
               if(xx>=0&&xx<=1000&&yy>=0&&yy<=1000&&w[xx][yy]!=1&&vis[xx][yy]==0)
               {
                   man k;k.x=xx;k.y=yy;k.step=t.step+1;q.push(k);vis[xx][yy]=1;
               }
           }
        }
    }
    int main()
    {
        int g,h;
        memset(vis,0,sizeof(vis));
        memset(w,0,sizeof(w));
        scanf("%d%d%d",&a,&b,&n);man s;
        a+=500;b+=500;
        for(int i=0;i<n;i++){scanf("%d%d",&g,&h);w[g+500][h+500]=1;}
       s.x=500;s.y=500;s.step=0;
        bfs(s);
        //if(flag==0)printf("-1
    ");
        return 0;
    }
    View Code
  • 相关阅读:
    macbook 无声音解决方案
    webapck dev server代理请求 json截断问题
    百度卫星地图开启
    服务器 nginx配置 防止其他域名绑定自己的服务器
    记一次nginx php配置的心路历程
    遇到npm报错read ECONNRESET怎么办
    运行svn tortoiseSvn cleanup 命令失败的解决办法
    svn add 命令 递归目录下所有文件
    m4出现Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
    Ubuntu下安装GCC,mpc、mpfr、gmp
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5703041.html
Copyright © 2011-2022 走看看