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





































  • 相关阅读:
    4星|《激荡十年,水大鱼大》:过去十年间国内商业简史
    4星|《三联生活周刊》2017年47期:所谓“嬉皮精神”,说白了就是让每一个人都能在不影响其他人的前提下,过自己想要的生活
    3星|《三联生活周刊》2017年48期:联大外文系主任叶公超让学生念一句英语就能判断出学生的籍贯
    3星|《终身成长》:成长型思维让人进步,固定型思维让人固步自封。有新意的励志书,但有锤子模式的嫌疑。
    4星|《癌症新知:科学终结恐慌》:非常新鲜的癌症科普
    PL/SQL Developer使用技巧
    Oracle数据导入导出imp/exp sp2-0734:未知的命令开头'imp...解决方法
    oracle数据库导入导出命令!
    如何完全卸载VS2010
    Oracle命令(一):Oracle登录命令
  • 原文地址:https://www.cnblogs.com/hrlsm/p/13331157.html
Copyright © 2011-2022 走看看