zoukankan      html  css  js  c++  java
  • HDU Problem 2612 Find a way 【BFS】

    Find a way

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

    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
     
    Author
    yifenfei
     
    Source
     
    Recommend
    yifenfei   |   We have carefully selected several similar problems for you:  1254 1728 2102 1072 1175 
     
    就因为一个字母的,没注意,WA了超十次。
    一开始我用“@”找Y,M,比较每次的最小值,结果超时。然后换战略,用Y,M找“@”,只用广搜两次,每次把搜到的结果储存一下。
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    struct node{
        int x, y, step;
    }a, b, temp;
    bool vis[300][300];
    char maze[300][300];
    int dx[4] = {0,1,0,-1};
    int dy[4] = {1,0,-1,0};
    int ans[300][300], ans1[300][300];
    int m, n, temp1, temp2, sx1, sx2, sy1, sy2;
    int bfs(int x, int y, bool flag) {
        queue<node> que;
        memset(vis, false, sizeof(vis));
        while (!que.empty()) que.pop();
        temp.x = x; temp.step = 0;
        temp.y = y;
        vis[x][y] = true;
        que.push(temp);
        while (que.size()) {
            a = que.front();
            que.pop();
            if (maze[a.x][a.y] == '@') {
                //找到后就记录结果。
                if (flag) ans1[a.x][a.y] = a.step;
                else ans[a.x][a.y] = a.step;
            }
            for (int i = 0; i < 4; i++) {
                b.x = a.x + dx[i];
                b.y = a.y + dy[i];
                b.step = a.step + 1;
                if (b.x < 0 || b.y < 0 || b.x >= n || b.y >= m || maze[b.x][b.y] == '#')
                    continue;
                if (vis[b.x][b.y]) continue;
                vis[b.x][b.y] = true;
                que.push(b);
            }
        }
    }
    int main() {
        while (scanf("%d%d", &n, &m) != EOF) {
            memset(ans, 0, sizeof(ans));
            memset(ans1, 0, sizeof(ans1));
            for (int i = 0; i < n; i++) {
                scanf("%s", maze[i]);
                for (int j = 0; j < m; j++) {
                    if (maze[i][j] == 'Y') {
                        sx1 = i, sy1 = j;
                    }
                    else if (maze[i][j] == 'M') {
                        sx2 = i, sy2 = j;
                    }
                }
            }
            bool flag = false;
            bfs(sx1, sy1, flag);
            bfs(sx2, sy2, flag = true);
            int cnt = 1e12;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    //必须这个点被同时访问到
                    if(ans1[i][j] != 0 && ans[i][j] != 0)
                        cnt = min(cnt, ans[i][j] + ans1[i][j]);
                }
            }
            printf("%d
    ", cnt*11);
        }
        return 0;
    }
  • 相关阅读:
    ubuntu 安装chrome浏览器
    ubuntu下Xmodmap映射Esc和Ctrl_L
    ubuntu 设置主屏和副屏
    maven 第一次运行报错
    Intellij Idea 配置并发布tomcat项目
    Nginx跨域设置
    Inotify+rsync实现实时数据同步
    Ubuntu-18.04设置开机启动脚本
    CentOS开机自启动/etc/rc.local不执行的解决办法
    ELK之使用kafka作为消息队列收集日志
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770888.html
Copyright © 2011-2022 走看看