• HDU 2612 Find a way bfs


    Description

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. 
    Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
    Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. 
     

    Input

    The input contains multiple test cases. 
    Each test case include, first two integers n, m. (2<=n,m<=200). 
    Next n lines, each line included m character. 
    ‘Y’ express yifenfei initial position. 
    ‘M’    express Merceki initial position. 
    ‘#’ forbid road; 
    ‘.’ Road. 
    ‘@’ KCF 
     

    Output

    For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
     

    Sample Input

    4 4
    Y.#@
    ....
    .#..
    @..M
    4 4
    Y.#@
    ....
    .#..
    @#.M
    5 5
    Y..@.
    .#...
    .#...
    @..M.
    #...#
     

    Sample Output

    66
    88
    66
     
              bfs:先从M,Y分别搜索到每个‘@’,并记录M,Y,到'@'的距离。最后两个for循环找最小值。
     
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    struct p
    {
        int x,y;
        int t;
    };p f,r;
    int n,m,ans;
    char map[205][205];
    int v1[205][205],v2[2][205][205];
    int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    void bfs(int x,int y,int k)
    {
        queue<p>q;
        while (!q.empty()) q.pop();
        f.x=x;
        f.y=y;
        f.t=0;
        int i;
        q.push(f);
        while (!q.empty())
        {
            r=q.front();
            q.pop();
            f.t=r.t+1;
            for (i=0;i<4;i++)
            {
                f.x=r.x+yi[i][0];
                f.y=r.y+yi[i][1];
                if (f.x>0&&f.x<=n&&f.y>0&&f.y<=m&&map[f.x][f.y]!='#'&&!v1[f.x][f.y])
                {
                    if (map[f.x][f.y]=='@') v2[k][f.x][f.y]=f.t;
                    v1[f.x][f.y]=1;
                    q.push(f);
                }
            }
        }
        return ;
    }
    int main()
    {
        int i,j,x1,x2,y1,y2;
        while (~scanf("%d%d",&n,&m))
        {
            x1=y1=x2=y2=1;
            for (i=1;i<=n;i++)
            for (j=1;j<=m;j++)
            {
                scanf(" %c",&map[i][j]);
                if (map[i][j]=='Y') {x1=i;y1=j;}
                if (map[i][j]=='M') {x2=i;y2=j;}
            }
            memset(v1,0,sizeof(v1));
            memset(v2,0,sizeof(v2));
            v1[x1][y1]=1;
            bfs(x1,y1,0);
            memset(v1,0,sizeof(v1));
            v1[x2][y2]=1;
            bfs(x2,y2,1);
            ans=1000000;
            for (i=1;i<=n;i++)
            for (j=1;j<=m;j++)
            {
                if (v2[0][i][j]&&v2[1][i][j])
                ans=min(ans,v2[0][i][j]+v2[1][i][j]);
            }
            printf("%d
    ",11*ans);
        }
    }
    
  • 相关阅读:
    快速免费用宝塔面板加开源小程序商城源码搭建自己的商城程序
    小程序商城,到底是购买源码好还是直接使用SaaS平台好?
    51单片机串口通信的注记
    关于vi 分屏的一些指令
    偶遇bash 的while read line 的问题
    centos 6 设置无密码登录ssh 不成功问题
    关于js框架 dwz 与 yii的的分页 以及筛选的结合
    完美解决百度地图MarkerClusterer 移动地图时,Marker 的Label 丢失的问题
    微信小程序购物商城系统开发系列-目录结构
    微信小程序支付步骤
  • 原文地址:https://www.cnblogs.com/pblr/p/4705329.html
Copyright © 2020-2023  润新知