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;
    }
  • 相关阅读:
    Javascript实现局部刷新
    Javascript模块化开发-轻巧自制
    javascript面向对象实例
    Javascript兼容和CSS兼容总结
    隐藏关机按钮
    数组排序
    常用数组获取最新和第一个元素值
    php 操作redis 以及几个常用命令
    git 常用命令
    JSON.parse和JSON.stringify的区别
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271269.html
Copyright © 2011-2022 走看看