【简单搜索】 HDU 2612 Find a way 详解

Find a way:

题目链接:https://vjudge.net/problem/HDU-2612

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.

 题面翻译:

 给你Y和M的初始位置,给你一副宁波的地图(本宁波人瑟瑟发抖),地图上有路,有kfc(可以作为路),有障碍物(不能走),问到哪个kfc能使两人加起来的耗时最短(走一步11分钟)

 分析:

 两个人分别广搜,求各自得到达每个kfc的最短用时并一起储存在一个数组中。搜索完后遍历数组找最短用时即可。

 AC代码:

#include<string.h>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

typedef struct{
    int x,y;
    int step;
}pos;


char ningbo[205][205];    //地图
int vis[205][205];       //标记点是否走过
int direct[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; //方向
int kfc[205][205];   //记录到达某个kfc的最短时间,不是kfc记为0
int n,m;

void bfs(pos start){
    memset(vis,0,sizeof(vis));
   queue<pos>q;
   q.push(start);
   while(!q.empty()){
      pos temp=q.front();
      if(ningbo[temp.x][temp.y]=='@')
         kfc[temp.x][temp.y]+=temp.step;
      q.pop();

      for(int i=0;i<4;i++){
           pos new_node=temp;
           new_node.x+=direct[i][0];
           new_node.y+=direct[i][1];
           new_node.step++;
           if(new_node.x<=n&&new_node.x>=1&&new_node.y<=m&&new_node.y>=1&&vis[new_node.x][new_node.y]==0&&ningbo[new_node.x][new_node.y]!='#'){
                    vis[new_node.x][new_node.y]=1;
                    q.push(new_node);
           }
      }
   }

}



int main()
{
    while(~scanf("%d%d",&n,&m)){
        memset(kfc,0,sizeof(kfc));
        pos Y,M;
        for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++){
              scanf(" %c",&ningbo[i][j]);
              if(ningbo[i][j]=='Y'){
                   Y.step=0;
                   Y.x=i,Y.y=j;
              }
              else if(ningbo[i][j]=='M'){
                   M.step=0;
                   M.x=i,M.y=j;
              }
           }
        bfs(M);
        bfs(Y);
        int ans=10000000;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(kfc[i][j]!=0)
                    ans=min(ans,kfc[i][j]);
        }
        printf("%d\n",11*ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值