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): 19320    Accepted Submission(s): 6234


    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
     
    #include <iostream>
    #include <queue>
    using namespace std;
    char a[205][205];
    int y[205][205],m[205][205];
    int d[4][2]={-1,0,1,0,0,-1,0,1};
    int n,k;
    
    int yx,yy,mx,my;
    queue<int>q;
    queue<int>qy;
    queue<int>qm;
    bool safe(int x,int y)
    {
        if(x>n||y>k||x<=0||y<=0) return 0;
        return 1;
    }
    int main()
    {
        while(cin>>n>>k)
        {
            int i,j;
            long long min=9999999;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=k;j++)
                {
                    cin>>a[i][j];
                    y[i][j]=-1;
                    m[i][j]=-1;
                    if(a[i][j]=='Y')
                    {
                        qy.push(i);
                        qy.push(j);
                        y[i][j]=0;
                    }
                    if(a[i][j]=='M')
                    {
                        qm.push(i);
                        qm.push(j);
                        m[i][j]=0;
                    }
                    if(a[i][j]=='@')
                    {
                        q.push(i);
                        q.push(j);
                    }
                }
            }
            while(!qy.empty()||!qm.empty())
            {
                if(!qy.empty())
                {
                    yx=qy.front();qy.pop();
                    yy=qy.front();qy.pop();
                    int vy=y[yx][yy];
                     for(i=0;i<4;i++)
                     { 
                         if(safe(yx+d[i][0],yy+d[i][1])&&a[yx+d[i][0]][yy+d[i][1]]!='#'&&y[yx+d[i][0]][yy+d[i][1]]==-1)
                         {
                             y[yx+d[i][0]][yy+d[i][1]]=vy+1;
                         qy.push(yx+d[i][0]);
                         qy.push(yy+d[i][1]);
                         }
                     }
                }
                if(!qm.empty())
                {
                    mx=qm.front();qm.pop();
                    my=qm.front();qm.pop();
                    int vm=m[mx][my];
                     for(i=0;i<4;i++)
                     { 
                         if(safe(mx+d[i][0],my+d[i][1])&&a[mx+d[i][0]][my+d[i][1]]!='#'&&m[mx+d[i][0]][my+d[i][1]]==-1)
                         {
                             m[mx+d[i][0]][my+d[i][1]]=vm+1;
                         qm.push(mx+d[i][0]);
                         qm.push(my+d[i][1]);
                         }
                     }
                }
            }
            int kx,ky;
            while(!q.empty())
            {
                kx=q.front();q.pop();
                ky=q.front();q.pop();
                if(y[kx][ky]==-1||m[kx][ky]==-1) continue;
                long long s=y[kx][ky]+m[kx][ky];
                if(s<min) min=s;
            }
            cout<<min*11<<endl;
        }
        return 0;
    }
  • 相关阅读:
    1.4(java学习笔记) 面向对象内存分析
    1.3(java学习笔记)构造方法及重载
    1.2(java学习笔记)类与对象
    1.1(java学习笔记) 面向过程与面向对象
    STM32F0库函数初始化系列:GPIO配置
    STM32F4库函数初始化系列:PWM输出
    STM32F4库函数初始化系列:DMA串口接收
    STM32F4库函数初始化系列:三重ADC——DMA
    STM32F1库函数初始化系列:DMA—ADC采集
    STM32F4库函数初始化系列:串口DMA接收
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8411170.html
Copyright © 2011-2022 走看看