zoukankan      html  css  js  c++  java
  • HDU

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612

    题目描述

    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.

    输入

    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

    输出

    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.

    样例输入

    4 4
    Y.#@
    ....
    .#..
    @..M
    4 4
    Y.#@
    ....
    .#..
    @#.M
    5 5
    Y..@.
    .#...
    .#...
    @..M.
    #...#

    样例输出

    66
    88
    66

    直接BFS模拟即可

    #include<bits/stdc++.h>
    
    
    #define REP(i,j,k) for(int i=j; i<k; i++)
    
    using namespace std;
    constexpr int MAX = 0x3f3f3f3f;
    int main(){
        // freopen("in.dat", "r", stdin);
        int n,m;
        int dir[4][2] = {{0,-1}, {-1, 0}, {1, 0}, {0,1}};
        while(cin>>n>>m){
            vector<string> maze(n);
            using pii=pair<int, int>;
            vector<vector<vector<int>>> ans(n, vector<vector<int>>(m, vector<int>{MAX,MAX}));
            REP(i,0,n) cin>>maze[i];
            queue<pii> que;
            auto valid = [&](int x, int y){
                return x>=0&&y>=0&&x<n&&y<m&&maze[x][y]!='#';
            };
            REP(i,0,n)
            REP(j,0,m)
            if(maze[i][j]=='Y'){
                que.push({i*10000+j, 0});
                ans[i][j][0]=0;
            }
            else if(maze[i][j]=='M'){
                que.push({i*10000+j, 1});
                ans[i][j][1]=0;
            }
            int x, y;
            while(!que.empty()){
                auto e = que.front();
                que.pop();
                for(int i=0; i<4; i++){
                    x = e.first/10000 + dir[i][0];
                    y = e.first%10000 + dir[i][1];
                    if(valid(x, y)&&ans[x][y][e.second]==MAX){
                        que.push({x*10000+y, e.second});
                        ans[x][y][e.second] = ans[e.first/10000][e.first%10000][e.second]+1;
                    }
                }
            }
            int res = MAX;
            REP(i,0,n)
            REP(j,0,m)
                if(maze[i][j]=='@')
                    res = min(res, ans[i][j][0]+ans[i][j][1]);
            cout<<res*11<<endl;
        }   
        return 0;
    }
    
  • 相关阅读:
    KMP
    837B. Balanced Substring
    JDK7和JDK8新特性
    数据库索引的实现原理
    mysql索引总结----mysql 索引类型以及创建
    Java 8新特性终极指南
    类加载机制
    类加载过程
    深入理解java虚拟机 精华总结(面试)(转)
    几种常用的设计模式介绍(转)
  • 原文地址:https://www.cnblogs.com/Crossea/p/13965998.html
Copyright © 2011-2022 走看看