zoukankan      html  css  js  c++  java
  • HDU

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 16184    Accepted Submission(s): 5194


    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
     
    Author
    yifenfei
     
    Source
     
    Recommend
    yifenfei

    题意:Y和M在两个不同起点,他们要到KFC集合,路上有多家KFC,问到哪家KFC能使他们的步数和最少?

    思路:两边bfs,分别存取Y和M到各家KFC的步数,相加求和,枚举各KFC输出最小值。

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    char a[205][205];
    int b[205][205],c[205][205];
    int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    
    struct Node{
        int x,y,s;
    }node;
    
    int main()
    {
        int n,m,yx,yy,mx,my,i,j;
        while(~scanf("%d%d",&n,&m)){
            memset(c,0,sizeof(c));
            queue<Node> q;
            for(i=0;i<n;i++){
                getchar();
                scanf("%s",a[i]);
                for(j=0;j<m;j++){
                    if(a[i][j]=='Y'){
                        yx=i;
                        yy=j;
                    }
                    if(a[i][j]=='M'){
                        mx=i;
                        my=j;
                    }
                }
            }
            memset(b,0,sizeof(b));
            b[yx][yy]=1;
            node.x=yx;
            node.y=yy;
            node.s=0;
            q.push(node);
            while(q.size()){
                for(i=0;i<4;i++){
                    int tx=q.front().x+t[i][0];
                    int ty=q.front().y+t[i][1];
                    if(tx<0||ty<0||tx>=n||ty>=m) continue;
                    if(a[tx][ty]=='#'||b[tx][ty]==1) continue;
                    b[tx][ty]=1;
                    if(a[tx][ty]=='@'){
                        c[tx][ty]=q.front().s+11;
                    }
                    node.x=tx;
                    node.y=ty;
                    node.s=q.front().s+11;
                    q.push(node);
                }
                q.pop();
            }
            memset(b,0,sizeof(b));
            b[mx][my]=1;
            node.x=mx;
            node.y=my;
            node.s=0;
            q.push(node);
            while(q.size()){
                for(i=0;i<4;i++){
                    int tx=q.front().x+t[i][0];
                    int ty=q.front().y+t[i][1];
                    if(tx<0||ty<0||tx>=n||ty>=m) continue;
                    if(a[tx][ty]=='#'||b[tx][ty]==1) continue;
                    b[tx][ty]=1;
                    if(a[tx][ty]=='@'){
                        c[tx][ty]+=q.front().s+11;
                    }
                    node.x=tx;
                    node.y=ty;
                    node.s=q.front().s+11;
                    q.push(node);
                }
                q.pop();
            }
            int min=1000000000;
            for(i=0;i<n;i++){
                for(j=0;j<m;j++){
                    if(c[i][j]<min&&c[i][j]!=0) min=c[i][j];
                }
            }
            printf("%d
    ",min);
        } 
        return 0;
    }
  • 相关阅读:
    LeetCode44——用搜索的思路去理解动态规划算法
    在vscode中配置LeetCode插件,从此愉快地刷题
    分布式专题——详解Google levelDB底层原理
    高等数学——求解不定积分经典换元法
    动态规划入门——详解完全背包与多重背包问题
    详解聚类算法Kmeans的两大优化——mini-batch和Kmeans++
    Python专题——详解enumerate和zip
    LeetCode42题,单调栈、构造法、two pointers,这道Hard题的解法这么多?
    零基础学Python--------第11章 使用Python操作数据库
    零基础学Python--------第10章 文件及目录操作
  • 原文地址:https://www.cnblogs.com/yzm10/p/7252706.html
Copyright © 2011-2022 走看看