• HDU 2612 Find a way 简单广搜


    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
    Sample Input
    4 4
    Y.#@
    ....
    .#..
    @..M
    4 4
    Y.#@
    ....
    .#..
    @#.M
    5 5
    Y..@.
    .#...
    .#...
    @..M.
    #...#
            
     
    Sample Output
    66
    88
    66 

    题意:

      两个人想在某家KCF见面,要求他们到这家KCF的时间和花费最少。#表示不能通过;.表示可以通过;@表示KFC;Y、M分别表示两个人

    思路:

      分别以两个人为起点,走一遍广搜,然后将所有的数值全部记录下来,最后逐个便利取最小值。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #define MAX 0x3f3f3f3f
    using namespace std;
    struct point
    {
        int x,y;
        int step;
    } node;
    int yx,yy,mx,my;
    queue<point> q;
    int vis[210][210];
    char map[210][210];
    int dir[4][2]= {-1,0, 1,0, 0,-1, 0,1};
    int dis1[210][210],dis2[210][210];
    int n,m;
    void bfs(point node,int dis[][210])
    {
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        q.push(node);
        point now;
        while(!q.empty())
        {
            node=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                now.x=node.x+dir[i][0];
                now.y=node.y+dir[i][1];
                now.step=node.step+1;
                if(now.x>=0&&now.x<=m&&now.y>=0&&now.y<=n)
                    if(map[now.x][now.y]!='#'&&vis[now.x][now.y]==0)
                    {
                        vis[now.x][now.y]=1;
                        dis[now.x][now.y]=now.step;
                        q.push(now);
                    }
            }
        }
    }
    int main()
    {
        while(~scanf("%d %d",&m,&n))
        {
            getchar();
            for(int i=0; i<m; i++)
            {
                for(int j=0; j<n; j++)
                {
                    scanf("%c",&map[i][j]);
                    //cin>>map[i][j];
                    if(map[i][j]=='Y')
                        yx=i,yy=j;
                    if(map[i][j]=='M')
                        mx=i,my=j;
                }
                getchar();
            }
            //以第一个人为起点走一遍广搜,dis1记录第一个人到所有点的距离
            node.x=yx,node.y=yy,node.step=0;
            bfs(node,dis1);
            //以第二个人为起点走一遍广搜,dis2记录第二个人到所有点的距离
            node.x=mx,node.y=my,node.step=0;
            bfs(node,dis2);
            
            int min=MAX;
            for (int i=0; i<m; i++)
                for (int j=0; j<n; j++)
                    if (map[i][j]=='@')
                        if (dis1[i][j]!=0&&dis2[i][j]!=0)
                            if(dis1[i][j]+dis2[i][j]<min)
                                min = dis1[i][j]+dis2[i][j];
            printf("%d
    ",min*11);
        }
    }
  • 相关阅读:
    Python sendemail txt,html,图片及附件
    python 3 requests库2个问题
    py37 unitest+html_sendmail
    python 3 unitest批量执行用例
    phthon 3 unittest模块使用
    python 之发送邮件
    ipad已停用 连接itunes怎么办
    Ubuntu 16.04系统挂载4T硬盘
    华硕RT-AC86U路由器 AP模式实现多路由器组网,扩展主路由器的无线网范围
    ubuntu 常用命令
  • 原文地址:https://www.cnblogs.com/aiguona/p/7269095.html
Copyright © 2020-2023  润新知