zoukankan      html  css  js  c++  java
  • hdu 2612 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

    题目大意

    两个人,一个M,一个Y,求两个人KFC(@)的最短时间。

    思路

    很明显的一道BFS的题,但是,出现了两个起点,那么可以进行两次的BFS,分别存下两个人的最短路径。最后找到两个人的最短路径之和。

    代码

    #include<iostream>
    #include<queue>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    int fx[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
    bool vis[205][205];
    char map[205][205];
    int dp[205][205][2];//分别记录两个人的最短路径
    int n, m;
    int flag;//当flag=0时,dp[][][flag]记录Y,当flag=1时,dp[][][flag]记录m。
    const int INF = 0x3f3f3f;
    
    struct node
    {
        int x, y, step;
    };
    
    
    void BFS(int x,int y){
        queue <node> q;
        node s, e;
        s.x = x;
        s.y = y;
        s.step = 0;
        vis[x][y] = true;
        q.push(s);
        while (!q.empty()){
            s = q.front();
            q.pop();
            for (int i = 0; i<4; i++)
            {
                e.x = s.x + fx[i][0];
                e.y = s.y + fx[i][1];
                if (e.x >= 0 && e.x<n&&e.y >= 0 && e.y<m&&!vis[e.x][e.y] && (map[e.x][e.y] == '@' || map[e.x][e.y] == '.'))
                {
                    vis[e.x][e.y] = 1;
                    e.step = s.step + 1;
                    if (map[e.x][e.y] == '@')
                        dp[e.x][e.y][flag] = min(dp[e.x][e.y][flag], e.step);//留下最小的路程
                    q.push(e);
                }
            }
        }
    }
    
    int main(){
    
        int i, j, cnt;
        while (cin >> n >> m){
            memset(dp, INF, sizeof(dp));
            for (i = 0; i<n; i++)
                scanf("%s", map[i]);
            for (i = 0; i < n; i++){
                for (j = 0; j < m; j++)
                {
                    if (map[i][j] == 'Y')
                    {
                        flag = 0;
                        memset(vis, 0, sizeof(vis));
                        BFS(i, j);
                    }
                    else if (map[i][j] == 'M')
                    {
                        flag = 1;
                        memset(vis, 0, sizeof(vis));
                        BFS(i, j);
                    }
                }
            }
            cnt = INF;
            for (i = 0; i < n; i++){
                for (j = 0; j<m; j++)
                {
                    if (map[i][j] == '@'&&cnt>dp[i][j][0] + dp[i][j][1])
                        cnt = dp[i][j][0] + dp[i][j][1];
                }
            }
            printf("%d
    ", cnt * 11);//cnt为最短路程,因为每步要11分钟,即为cnt*11;
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    MySQL回顾
    mysql多表查询
    通过JDBC进行简单的增删改查(以MySQL为例)
    如何正确学习JavaScript
    List集合遍历时修改元素出现并发修改异常总结
    国内有哪些质量高的JAVA社区?
    【题解】【链表】【Leetcode】Add Two Numbers
    【题解】【字符串】【Leetcode】Valid Palindrome
    【题解】【DP】【Leetcode】Climbing Stairs
    【题解】【数组】【Leetcode】Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/pcdl/p/13321949.html
Copyright © 2011-2022 走看看