Find a way HDU - 2612

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

题意:Y和M约定都到@位置,@有多个,保证Y和M一定有一个@都能到达,求Y和M到达@的最短时间;

思路,分别从Y,M开始搜索,计算到达每个@位置的最短距离,相加之后求最短距离*11(每走一步11分钟);

注意:因为可能出现不能到达@位置的情况,因此ans数组初始化时不能为0;

#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#define MAXN 205
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int vast[MAXN][MAXN];
int vist[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
char mp[MAXN][MAXN];
int ans1[MAXN][MAXN],ans2[MAXN][MAXN];
int n,m;
struct node
{
    int x,y;
    int sum;
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vast[x][y]&&mp[x][y]!='#')
        return 1;
    return 0;
}
void bfs1(int x,int y)
{
    queue<node>s;
    while(!s.empty()) s.pop();
    node xy,xz;
    xy.x=x;
    xy.y=y;
    xy.sum=0;
    s.push(xy);
    while(!s.empty())
    {
        xy=s.front();
        s.pop();
        if(mp[xy.x][xy.y]=='@')
        {
            ans1[xy.x][xy.y]=xy.sum;
        }
        for(int i=0;i<4;i++)
        {
            int xi=xy.x+vist[i][0];
            int yi=xy.y+vist[i][1];
            if(judge(xi,yi))
            {
                vast[xi][yi]=1;
                xz.x=xi;
                xz.y=yi;
                xz.sum=xy.sum+1;
                s.push(xz);
            }
        }
    }
    return ;
}
void bfs2(int x,int y)
{
    queue<node>s;
    while(!s.empty()) s.pop();
    node xy,xz;
    xy.x=x;
    xy.y=y;
    xy.sum=0;
    s.push(xy);
    while(!s.empty())
    {
        xy=s.front();
        s.pop();
        if(mp[xy.x][xy.y]=='@')
        {
            ans2[xy.x][xy.y]=xy.sum;
        }
        for(int i=0;i<4;i++)
        {
            int xi=xy.x+vist[i][0];
            int yi=xy.y+vist[i][1];
            if(judge(xi,yi))
            {
                vast[xi][yi]=1;
                xz.x=xi;
                xz.y=yi;
                xz.sum=xy.sum+1;
                s.push(xz);
            }
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int x1,y1,x2,y2;
        for(int i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='Y')
                {
                    x1=i,y1=j;
                }
                else if(mp[i][j]=='M')
                {
                    x2=i,y2=j;
                }
            }
        }
        memset(ans1,inf,sizeof(ans1));//不能为0
        memset(ans2,inf,sizeof(ans2));
        memset(vast,0,sizeof(vast));
        vast[x1][y1]=1;
        bfs1(x1,y1);
        memset(vast,0,sizeof(vast));
        vast[x2][y2]=1;
        bfs2(x2,y2);
        int minn=inf;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='@')
                {
                    minn=min(minn,ans1[i][j]+ans2[i][j]);
                }
            }
        }
        printf("%d\n",minn*11);
    }
    return 0;
}








阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/acmliying/article/details/79980086
个人分类: 搜索
上一篇CodeForces - 612D G The Union of k-Segments
下一篇排列组合 &quot;n个球放入m个盒子m&quot;问题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭