zoukankan      html  css  js  c++  java
  • HDU 2612 Find a way

    Find a way

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


    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


    这个题目是知道两个人的所在位置,然后求两人到达KFC的总时间最少的时间是多少。

    一开始没有考虑到KFC也可以当路,想如果一个人到达肯德基以后等待另一个人的方案。这样确实不对。

    所以直接两遍BFS,维护两个二维数组,存人到KFC的时间,然后最后遍历所有KFC的地方,取出最小的两人的总和即可

    /*************************************************************************
    	> File Name: Find_a_way.cpp
    	> Author: Zhanghaoran0
    	> Mail: chiluamnxi@gmail.com
    	> Created Time: 2015年08月19日 星期三 08时43分00秒
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    char map[205][205];
    int ystep[205][205];
    int mstep[205][205];
    bool vis[205][205];
    int inf = 1000001;
    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};
    struct node{
        int x, y;
        int step;
    };
    int n, m;
    void bfs(int x, int y, int flag){
        queue<node> q;
        node last, now;
        last.x = x;
        last.y = y;
        last.step = 0;
        vis[x][y] = true;
        q.push(last);
        while(!q.empty()){
            last = q.front();
            q.pop();
            now.step = last.step + 1;
            for(int i = 0; i < 4; i ++){
                int xx = last.x + dx[i];
                int yy = last.y + dy[i];
                
                if(vis[xx][yy] || xx < 0 || yy < 0 || xx >= n || yy >= m || map[xx][yy] == '#')
                    continue;
    
                now.x = xx;
                now.y = yy;
                
                vis[xx][yy] = true;
                if(map[xx][yy] == '@' && flag == 0){
                    ystep[xx][yy] = now.step;
                }
                else if(map[xx][yy] == '@' && flag == 1){
                    mstep[xx][yy] = now.step;
                }
    
                q.push(now);
                    
            }
        }
    }
    
    int main(void){
        while(scanf("%d%d", &n, &m) != EOF){
            int min = 1000001; 
            for(int i = 0; i < n; i ++){
                for(int j = 0; j < m; j ++){
                    ystep[i][j] = inf;
                    mstep[i][j] = inf;
                }
            }
    
            for(int i = 0; i < n; i ++){
                scanf("%s", map[i]);
            }
    
            for(int i = 0; i < n; i ++){
                for(int j = 0; j < m; j ++){
                    if(map[i][j] == 'Y'){
                        memset(vis, 0, sizeof(vis));
                        bfs(i, j, 0);
                    }
                    else if(map[i][j] == 'M'){
                        memset(vis, 0, sizeof(vis));
                        bfs(i, j, 1);
                    }
                }
            }
    
            for(int i = 0; i < n; i ++){
                for(int j = 0; j < m; j ++){
                    if(map[i][j] =='@' && min > ystep[i][j] + mstep[i][j])
                        min = ystep[i][j] + mstep[i][j];
                }
            }
    
            cout << min * 11 << endl;
        }
        return 0;
    }
    


  • 相关阅读:
    Delphi 10.1.2 berlin开发跨平台APP的几点经验
    (矫情)关于编程(能力第一,其它不要多想)
    Xdite:永葆热情的上瘾式学习法(套路王:每天总结自己,反省自己的作息规律,找到自己的幸运时间、幸运方法,倒霉时间、倒霉方法。幸运是与注意力挂钩的。重复才能让自己登峰造极,主动去掉运气部分来训练自己。游戏吸引自己的几个原因非常适合训练自己)good
    无锡美新赵阳:创业18年,一辈子做好一家企业(创业是一种生活方式;为了赚钱而创业,那是扯淡”。最重要的是做自己喜欢做的事情)
    Delphi中流对象的应用
    多样性的配置来源
    WeText项目的服务端
    集线器,交换机,路由器
    不知道的JavaScript
    IPerf网络测试工具
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136088.html
Copyright © 2011-2022 走看看