HDU 2612 Find a way【双重BFS】

本文介绍了一个利用BFS算法解决的实际问题:计算两个人从各自位置出发到同一地点见面所需的最短时间。通过遍历地图上的所有肯德基门店并计算两人到达每个门店的总时间来找到最优解。

思路还是BFS,有些人用双向队列了,

然而弱不会用,

弱只能用一个BFS函数暴力两个搜索;

然后比较;

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      


AC代码如下:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n,m,count1[210][210],count2[210][210],visited[210][210];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char str[210][210];
struct node{
    int x;
    int y;
};
bool check(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&str[x][y]!='#'&&visited[x][y]==0)
        return true;
    else
        return false;
}
void bfs(int flag,int x,int y)  //*这里是遍历到达所有点的时间,然后main函数将到达KFC的时间相加比较
{
    int f=0,r=0,t1,t2;
    node p,temp;
    queue<node>q;
    p.x=x;
	p.y=y;
    q.push(p);
    if(flag==1)
    {
        while(!q.empty())
        {
          p=q.front();
          q.pop();
          for(int i=0;i<4;i++)
          {
              t1=p.x+dir[i][0];
	      t2=p.y+dir[i][1];
              if(check(t1,t2))
              {
                 visited[t1][t2]=-1;
                 temp.x=t1;
                 temp.y=t2;
                 q.push(temp);
                 count1[t1][t2]=count1[p.x][p.y]+11;
              }
          }
        }
    }
    else
    {
        while(!q.empty())
        {
          p=q.front();
          q.pop();
          for(int i=0;i<4;i++)
          {
              t1=p.x+dir[i][0];
	      t2=p.y+dir[i][1];
              if(check(t1,t2))
              {
                 visited[t1][t2]=-1;
                 temp.x=t1;
                 temp.y=t2;
                 q.push(temp);
                 count2[t1][t2]=count2[p.x][p.y]+11;
              }
          }
        }
    }
}
int main()
{
    int i,j,x1,y1,x2,y2,a[40005][2],num,flag,min;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        memset(a,0,sizeof(a));
        memset(count1,0,sizeof(count1));
        memset(count2,0,sizeof(count2));
        num=0;
	min=100000000;
        for(i=0;i<n;i++)
            scanf("%s",str[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(str[i][j]=='Y')
                {
                  x1=i;
                  y1=j;
                }
                else if(str[i][j]=='M')
                {
                  x2=i;
                  y2=j;
                }
                else if(str[i][j]=='@')
                {
                  a[num][0]=i;
                  a[num][1]=j;
                  num++;
                }
            }
        }
        memset(visited,0,sizeof(visited));
        flag=1;
        
        bfs(flag,x1,y1);
        memset(visited,0,sizeof(visited));
        flag=2;
        
        bfs(flag,x2,y2);
        for(i=0;i<num;i++)
        {
            if(count1[a[i][0]][a[i][1]]!=0&&count2[a[i][0]][a[i][1]]!=0)//*必须是Y和M都能到达的
            {
                if(min>(count1[a[i][0]][a[i][1]]+count2[a[i][0]][a[i][1]]))
                   min=count1[a[i][0]][a[i][1]]+count2[a[i][0]][a[i][1]];
            }
        }
        printf("%d\n",min);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kelisita

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值