hdu 2612(两个方向广搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3786    Accepted Submission(s): 1232


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
 
思路:从S点搜索整个图,并且求出S点到每个KFC的距离,然后从M点搜索整个图,并且求出M点到每个KFC的距离,然后求距离最小和即可;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#define judge(x,y) x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&map[x][y]!='#'
using namespace std;

struct node
{
    int x,y,step;
};

int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};

char map[220][220];
int dis[220][220][2],vis[220][220];
int n,m,flag;

void bfs(int x,int y)
{
   memset(vis,0,sizeof(vis));
   queue<node>que;
   node cur,next;
   cur.x=x;cur.y=y;cur.step=0;
   vis[x][y]=1;
   que.push(cur);
   while(!que.empty())
   {
       cur=que.front();
       que.pop();
       next.step=cur.step+1;
       for(int k=0;k<4;k++)
       {
           next.x=x=cur.x+xx[k];
           next.y=y=cur.y+yy[k];
           if(judge(x,y))
           {
               vis[x][y]=1;
               if(map[x][y]=='@')
                   dis[x][y][flag]=next.step;
               que.push(next);
           }
       }
   }
}

int main()
{
        while(cin>>n>>m)
        {
            for(int i=0;i<=n;i++)
             for(int j=0;j<=m;j++)
              dis[i][j][0]=dis[i][j][1]=99999999; //这个地方如果初始化为0,就错了,我还想了好久为啥错=。=ORZ,
              //那是因为在最后的判断过程中,有些KFC是S点和M点都无法到达的~所以判断过程中自然要设置成正无穷
            for(int i=1;i<=n;i++)
              for(int j=1;j<=m;j++)
               cin>>map[i][j];

            for(int i=1;i<=n;i++)
              for(int j=1;j<=m;j++)
              {

                if(map[i][j]=='Y')
                {
                    flag=0;
                    bfs(i,j);
                }
                else if(map[i][j]=='M')
                {

                    flag=1;
                    bfs(i,j);
                }
              }
              int cnt=99999999;
              for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                {
                  if(map[i][j]=='@')cnt=min(cnt,dis[i][j][0]+dis[i][j][1]);
                }
              cout<<cnt*11<<endl;
        }
        return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值