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;
    }
    


  • 相关阅读:
    网页安装ipa
    windows开通https服务
    Asp.Net上传大文件带进度条swfupload
    Asp.Net采集网页方法大全(5种)
    asp.net上传大文件-请求筛选模块被配置为拒绝超过请求内容长度的请求
    在IIS服务器上屏蔽IP的访问
    网络广告CPS/CPC/CPV/CPM/CPA分别是什么意思
    Asp.Net判断字符是否为汉字的方法大全
    Asp.Net使用代理IP远程获取数据
    Asp.Net保存session的三种方法
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136088.html
Copyright © 2011-2022 走看看