• Find a way solution


    question:

    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.

    InputThe 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
    OutputFor 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

    两次bfs,分别计算出到KFC要花费的时间,然后相加。注意有人可能到达不了KFC。
    #include <cstdio>
    #include <cstring>
    #include <queue>
    //#include <algorithm>
    using namespace std;
    const int INF = 1e7 + 7;
    const int maxx = 210;
    char g[maxx][maxx];
    int used[maxx][maxx];
    int t1[maxx][maxx];
    int t2[maxx][maxx]; 
    int n, m;
    struct node{
        int x, y;
        node(int i = 0, int j = 0) : x(i), y(j) {};
    };
    int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
    queue<node> que;    
    void bfs1(int x, int y)     // 从第一个起点出发记录到每点的时间
    {
        memset(used, 0, sizeof(used));
        memset(t1, 0, sizeof(t1));
        while(!que.empty()) que.pop();
        que.push(node(x, y));
        used[x][y] = 1;   //用来标记 
        t1[x][y] = 0;   //记录时间 
        while( !que.empty())
        {
            node now = que.front(); que.pop();
            for(int i=0; i<4; i++)
            {
                int dx = now.x + d[i][0];
                int dy = now.y + d[i][1];
                if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
                {
                    t1[dx][dy] = t1[now.x][now.y] + 1;    // 到下一步的时间加一
                    used[dx][dy] = 1;   //标记 
                    que.push(node(dx, dy));
                }
            }
        }
        for(int i=0; i<n; i++)  // 不能到达的点记为无穷大
        {
            for(int j=0; j<m; j++)
            {
                if(t1[i][j] == 0)
                    t1[i][j] = INF;
            }
        }
    }
    void bfs2(int x, int y)     
    {
        memset(used, 0, sizeof(used));
        memset(t2, 0, sizeof(t2));
        while( !que.empty())    que.pop();
        que.push(node(x, y));
        used[x][y] = 1;
        t2[x][y] = 0;
        while(!que.empty())
        {
            
            node now = que.front(); que.pop();
            for(int i=0; i<4; i++)
            {
                int dx = now.x + d[i][0];
                int dy = now.y + d[i][1];
                if(dx >= 0 && dx < n && dy >= 0 && dy < m && g[dx][dy] != '#' && !used[dx][dy])
                {
                    t2[dx][dy] = t2[now.x][now.y] + 1;
                    used[dx][dy] = 1;
                    que.push(node(dx, dy));
                }
            }
        }
        for(int i=0; i<n; i++)   //不能到达的点记为无穷 
            for(int j=0; j<m; j++)
                if(t2[i][j] == 0)
                    t2[i][j] = INF;
    }
    int main()
    {
        while(scanf("%d %d", &n, &m) != EOF)
        {
            for(int i=0; i<n; i++)
                scanf("%s", g[i]);
            int x1, y1, x2, y2;
            for(int i=0; i<n; i++)  // 寻找两个起点
            {
                for(int j=0; j<m; j++)
                {
                    if(g[i][j] == 'Y')
                         x1 = i, y1 = j;    
                    else if(g[i][j] == 'M')
                        x2 = i, y2 = j;
                }
            }
            bfs1(x1, y1);
            bfs2(x2, y2);
            int ans = INF;
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    if(g[i][j] == '@')  // 每次取总时间最少的
                        ans = min(ans, t1[i][j] + t2[i][j]);
                }
            }
            printf("%d
    ", ans * 11);
        }
        return 0;
    }





































  • 相关阅读:
    arr.forEach()与for...in的用法举例
    git
    hql查询
    JAVA Hibernate工作原理及为什么要用
    mysql中key 、primary key 、unique key 与index区别
    aop
    hibernate json数据死循环
    nginx 转帖
    Maven搭建web项目
    ajaxfileupload 附加参数
  • 原文地址:https://www.cnblogs.com/hrlsm/p/13331157.html
Copyright © 2020-2023  润新知