hdu 2612 Find a way

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

 

题意:Y,M是两个人,他们要到其中一个@见面,让你求出到他俩的距离之和最短的@,并输出需要的时间


思路:一看题目非常直接bfs ,刚开始我的代码是遍历每个@然后bfs他到Y  M的距离,并求出最小值,但交了一发TLE,然后我又开始分析了题目,发现自己的思路非常的智障,如果@非常多那岂不是要搜索好多好多好多好多遍,受不了了,然后发现我们完全可以根据确定的Y 和M的位置搜索啊,这样只需要搜索两遍就可以了啊,每次搜索的过程中记录一下Y(M)到所能到达的各个点的最短路径的长度,然后遍历@求Y M到其距离的最小值即可


ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int x,y,k,h,vis1[205][205],vis2[205][205],ans1[205][205],ans2[205][205];
char a[300][300];
int cnx[10]={0,-1,1,0};
int cny[10]={-1,0,0,1};
struct node
{
    int x1;int y1;
    int p;
};
int bfs(int m,int n,int vis[205][205],int ans[205][205])
{
   memset(vis,0,sizeof(vis));
   queue<node> que;
   node head,next;
   head.x1=m;
   head.y1=n;
   head.p=0;
   vis[m][n]=1;
   ans[m][n]=0;
   que.push(head);
   while(!que.empty())
   {
       head=que.front();
       que.pop();
       for(int i=0;i<4;i++)
       {
           int m1=head.x1+cnx[i];
           int n1=head.y1+cny[i];
           if(m1<0||m1>=x||n1<0||n1>=y||vis[m1][n1]==1||a[m1][n1]=='#')
           continue;
           next.x1=m1;
           next.y1=n1;
           next.p=head.p+1;
           ans[m1][n1]=ans[head.x1][head.y1]+1;
           que.push(next);
           vis[m1][n1]=1;
       }
   }
   return 0;
}
int main()
{
    int xx,xy,yx,yy;
    while(cin>>x>>y)
    {
        for(int i=0;i<x;i++)
        scanf("%s",a[i]);
        for(int i=0;i<x;i++)
        {
          for(int j=0;j<y;j++)
          {
            if(a[i][j]=='Y')
            {
             xx=i;xy=j;
            }
            if(a[i][j]=='M')
            {
                yx=i;yy=j;
            }
          }
        }
        memset(vis1,0,sizeof(vis1));
        memset(ans1,0,sizeof(ans1));
        bfs(xx,xy,vis1,ans1);
        memset(vis2,0,sizeof(vis2));
        memset(ans2,0,sizeof(ans2));
        bfs(yx,yy,vis2,ans2);
        int minn=100000000;
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<y;j++)
            {
                if(a[i][j]=='@'&&ans1[i][j]&&ans2[i][j])
                {
                  if(ans1[i][j]+ans2[i][j]<minn)
                  minn=ans1[i][j]+ans2[i][j];
                }
            }
        }
        cout<<minn*11<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值