您的位置:首页 > 其它

hdu 2612 Find a way

2017-03-21 20:11 351 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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

 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

 

[align=left]Sample Output[/align]

66
88
66

 

[align=left]Author[/align]
yifenfei
 

[align=left]Source[/align]
奋斗的年代

 

题意: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]
=1;

   ans[m]
=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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: