zoukankan      html  css  js  c++  java
  • 杭电搜索 2612 Find a way

    Find a way

    Problem 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
    这道题 也不是很难    就是  双查找  找到 两人到达@的时间记录下来 最后相加作比较  求时间最少的  就是本题的解

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    struct node
    {
        int x,y,num;
    }as,x1,x2,t;
    int n,m,n1,m1,k,l;
    char w[201][201];
    int w1[201][201];
    int w2[201][201];
    int dd[201][201];
    int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
    void dfs(int e)
    {
        memset(dd,0,sizeof(dd));
        queue<node>q;
        if(e==0)//判断是Y  还是M   选择入队
            q.push(x1);
        else
            q.push(x2);
        while(!q.empty())
        {
            as=q.front();
            q.pop();
            dd[as.x][as.y]=1;
            for(int i=0;i<4;i++)
            {
                t.x=as.x+dir[i][0];
                t.y=as.y+dir[i][1];
                t.num=as.num+11;
                if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&dd[t.x][t.y]==0)
                {
                    if(w[t.x][t.y]=='@')
                    {
                        if(e==0)
                            w1[t.x][t.y]=t.num;
                        else
                            w2[t.x][t.y]=t.num;
                        q.push(t);
                    }
                    else if(w[t.x][t.y]=='.')
                        q.push(t);
                    dd[t.x][t.y]=1;
                }
            }
    
        }
    }
    int main()
    {
        while(cin>>n>>m)
        {
            memset(w,0,sizeof(w));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin>>w[i][j];
                    if(w[i][j]=='Y')
                    {
                        x1.x=i;
                        x1.y=j;
                        x1.num=0;
                    }
                    if(w[i][j]=='M')
                    {
                        x2.x=i;
                        x2.y=j;
                        x2.num=0;
                    }
                }
            }
            memset(w1,0,sizeof(w1));
            dfs(0);
            memset(w2,0,sizeof(w2));
            dfs(1);
            int ans=9999999;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(w1[i][j]!=0&&w2[i][j]!=0)//排除不是@的情况  
                    ans=ans<(w1[i][j]+w2[i][j])?ans:(w1[i][j]+w2[i][j]);//比较大小 找小的
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    JDK1.8HashMap底层实现原理
    关于map转json,空key丢失的问题
    spring一些注解的使用及相关注解差异
    搭建基础项目遇到的一些小坑
    解析ftp上word文档的文字并输入
    R语言中回归模型预测的不同类型置信区间应用比较分析
    R语言中的广义线性模型(GLM)和广义相加模型(GAM):多元(平滑)回归分析保险资金投资组合信用风险敞口
    R语言对巨灾风险下的再保险合同定价研究案例:广义线性模型和帕累托分布Pareto distributions分析
    R语言中GLM(广义线性模型),非线性和异方差可视化分析
    如何用R语言绘制生成正态分布图表
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900181.html
Copyright © 2011-2022 走看看