HDU 2612 Find a way

本文介绍了一种使用双起点BFS寻路算法解决寻找最佳会面地点的问题。通过分别从两个起点进行广度优先搜索,记录到达每个点的距离,并在遍历结束后选取距离之和最小的KFC作为会面地点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:Y和M两个人要在KCF约约约,找一个家离两个人都比较近的KFC,输出两个人所用的时间之和(每格11分钟)。输入2个整数代表输入地图的行列数。接下来输入地图。Y、M分别代表Y、M的起始位置。'.'是路,'#'是墙,'@'表示KFC的位置。

其实我们要做的就是对两个人分别进行两次BFS,记录步数,最后输出的时候再x11就ok啦。因为会有多个KFC,所以定了两个二维数组dis1、dis2,分别代表Y和M到达地图的某个位置上时的步数。需要注意的是除了'#'的位置不能走,其它的位置都是可以通过的,也就是说KFC也可以当成路来走,判断某点是否可行的时候需要注意~

Find a way

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


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

题目链接:HDU_2612 Find a way

下面附上AC代码:

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 205
int n,m,dis1[N][N],dis2[N][N],vis[N][N];  //dis1、dis2分别记录Y、M到达某点的步数
int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char maze[N][N];

struct node{
    int x;
    int y;
};

int judge(int x,int y)
{
    if(x>=0 &&x<n && y>=0 && y<m && maze[x][y]!='#' && vis[x][y]==0) //判断某点是否可行
        return 1;
    else
        return 0;
}

void bfs(int flag,int x,int y)
{
    int f=0,r=0,t1,t2;
    node cur,temp;
    queue<node>Q;
    cur.x=x;
    cur.y=y;
    Q.push(cur);
    if(flag==1)   //flag1对应Y的BFS
    {
        while(!Q.empty())
        {
          cur=Q.front();
          Q.pop();
          for(int i=0;i<4;i++)
          {
              t1=cur.x+to[i][0];
              t2=cur.y+to[i][1];
              if(judge(t1,t2))
              {
                 vis[t1][t2]=-1;
                 temp.x=t1;
                 temp.y=t2;
                 Q.push(temp);
                 dis1[t1][t2]=dis1[cur.x][cur.y]+1;
              }
          }
        }
    }
    else   //flag2对应M的BFS,区别在于dis1、dis2上。定成3维数组把两个合到一起也可以
    {
        while(!Q.empty())
        {
          cur=Q.front();
          Q.pop();
          for(int i=0;i<4;i++)
          {
              t1=cur.x+to[i][0];t2=cur.y+to[i][1];
              if(judge(t1,t2))
              {
                 vis[t1][t2]=-1;
                 temp.x=t1;
                 temp.y=t2;
                 Q.push(temp);
                 dis2[t1][t2]=dis2[cur.x][cur.y]+1;
              }
          }
        }
    }
}

int main()
{
    int i,j,x1,y1,x2,y2,a[40005][2],num,flag,minn;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        num=0;minn=100000000;
        for(i=0;i<n;i++)
            scanf("%s",maze[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                dis1[i][j]=0;  //初始化
                dis2[i][j]=0;
                vis[i][j]=0;
                if(maze[i][j]=='Y') //x1y1记录Y位置
                {
                  x1=i;
                  y1=j;
                }
                else if(maze[i][j]=='M')  //x2y2记录M位置
                {
                  x2=i;
                  y2=j;
                }
                else if(maze[i][j]=='@') //a数组记录KCF位置
                {
                  a[num][0]=i;
                  a[num][1]=j;
                  num++;
                }
            }
        }
        /*对Y和M进行BFS*/
        flag=1;
        vis[x1][y1]=-1;
        bfs(flag,x1,y1);
        memset(vis,0,sizeof(vis)); //对Y进行BFS后,把vis复原
        flag=2;
        vis[x2][y2]=-1;
        bfs(flag,x2,y2);

        for(i=0;i<num;i++)
        {
            if(dis1[a[i][0]][a[i][1]]!=0&&dis2[a[i][0]][a[i][1]]!=0) //如果KCF所在位置步数不为0,即两人都能到达该位置
            {
                if(minn>(dis1[a[i][0]][a[i][1]]+dis2[a[i][0]][a[i][1]]))
                   minn=dis1[a[i][0]][a[i][1]]+dis2[a[i][0]][a[i][1]];
            }
        }
        printf("%d\n",minn*11);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值