zoukankan      html  css  js  c++  java
  • HDU 2612 Find a way

    Find a way

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

    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
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int x11,y11,x22,y22;
    char map[210][210];
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    int n,m,ans=0x7f7f7f7f;
    int vis[201][201],an[201][201][2];
    void bfs(int x,int y,int opt){
        queue<int>quex,quey,step;
        memset(vis,0,sizeof(vis));
        quex.push(x);quey.push(y);
        step.push(0);vis[x][y]=1;
        while(!quex.empty()){
            int nowx=quex.front();quex.pop();
            int nowy=quey.front();quey.pop();
            int nows=step.front();step.pop();
            if(map[nowx][nowy]=='@')    an[nowx][nowy][opt]=nows;
            for(int i=0;i<4;i++){
                int cx=nowx+dx[i];
                int cy=nowy+dy[i];
                int cs=nows+11;
                if(cx>=1&&cx<=n&&cy>=1&&cy<=m&&map[cx][cy]!='#'&&!vis[cx][cy]){
                    quex.push(cx);quey.push(cy);
                    step.push(cs);vis[cx][cy]=1;
                }
            }
        }
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++){
                    cin>>map[i][j];
                    if(map[i][j]=='M'){ x11=i;y11=j; }
                    if(map[i][j]=='Y'){ x22=i;y22=j; }
                }
            bfs(x11,y11,0);bfs(x22,y22,1);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                    if(vis[i][j]&&map[i][j]=='@')
                        ans=min(ans,an[i][j][0]+an[i][j][1]);
            printf("%d
    ",ans);
            ans=0x3f3f3f3f;
            //memset(an,0x7f,sizeof(an));
        }
    } 
  • 相关阅读:
    OCP 062【中文】考试题库(cuug内部资料)第29题
    413. 等差数列划分 力扣(中等) 找规律,细节
    264. 丑数 II 力扣(中等) 动态规划,不会
    313. 超级丑数 力扣(中等) 动态规划,不会做
    5840. 使字符串平衡的最小交换次数 力扣(中等) 第255场oppo周赛 猜出来的
    手写一个仿微信登录的Nodejs程序
    你不知道的CSS国际化
    React实现类似淘宝tab居中切换效果
    原来 CSS 这样写是会让 App 崩溃的
    css中class和id之间有什么区别?
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/9911000.html
Copyright © 2011-2022 走看看