HDU 2612 Find a way(双向bfs)

题目代号: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): 15919    Accepted Submission(s): 5110


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

题目大意:有两个人,位置分别由Y与M代替,@是餐厅的位置,每个人移动到周围的点需要11分钟,现在求两个人到达某个餐厅的时间花费最少。

题目思路:双向bfs遍历所有的位置,分别用两个数组作为标记,如果碰到@就保存当前花费的步数,然后两个数组对应位置相加,然后找到总步数最小的那个的值乘以11,输出就行了

AC代码:

复制代码
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;

const int MAXM=205;
char a[MAXM][MAXM];
int n,m;
int cx[]= {-1,1,0,0};
int cy[]= {0,0,-1,1};

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

queue<node>Q;
int vis1[MAXM][MAXM];
int vis2[MAXM][MAXM];

void bfs()
{
    while(!Q.empty())
    {
        int x=Q.front().x;
        int y=Q.front().y;
        int flag=Q.front().flag;
        int cnt=Q.front().step;
        Q.pop();
        for(int i=0; i<4; i++)
        {
            int tx=x+cx[i];
            int ty=y+cy[i];
            if(flag==1)
            {
                if(vis1[tx][ty]==-1)
                {
                    if(a[tx][ty]=='@')
                    {
                        vis1[tx][ty]=cnt+1;
                    }
                    else
                    {
                        vis1[tx][ty]=0;
                    }
                    Q.push(node{tx,ty,1,cnt+1});
                }
            }
            else
            {
                if(vis2[tx][ty]==-1)
                {
                    if(a[tx][ty]=='@')
                    {
                        vis2[tx][ty]=cnt+1;
                    }
                    else
                    {
                        vis2[tx][ty]=0;
                    }
                    Q.push(node{tx,ty,2,cnt+1});
                }
            }
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d%d",&n,&m))
    {
        mem(vis1,0);
        mem(vis2,0);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",a[i]+1);
            for(int j=1; j<=m; j++)
            {
                if(a[i][j]=='Y')
                {
                    Q.push(node{i,j,1,0});
                    vis1[i][j]=vis2[i][j]=-1;
                }
                else if(a[i][j]=='M')
                {
                    Q.push(node{i,j,2,0});
                    vis1[i][j]=vis2[i][j]=-1;
                }
                else if(a[i][j]=='.'||a[i][j]=='@')
                {
                    vis1[i][j]=vis2[i][j]=-1;
                }
            }
        }
        bfs();
/*
        FOR(i,1,n)
        {
            FOR(j,1,m)
            {
                printf("%3d",vis1[i][j]);
            }
            puts("");
        }
        puts("");
        FOR(i,1,n)
        {
            FOR(j,1,m)
            {
                printf("%3d",vis2[i][j]);
            }
            puts("");
        }
*/
        int ans=100000;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                 vis1[i][j]+=vis2[i][j];
                 if(vis1[i][j]>0)ans=min(ans,vis1[i][j]);
            }
        }
        cout<<ans*11<<endl;
    }
    return 0;
}
复制代码

 

posted @   韵祈  阅读(165)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示