Find a way HDU - 2612

23人阅读 评论(0) 收藏 举报
分类:

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约定都到@位置,@有多个,保证Y和M一定有一个@都能到达,求Y和M到达@的最短时间;

思路,分别从Y,M开始搜索,计算到达每个@位置的最短距离,相加之后求最短距离*11(每走一步11分钟);

注意:因为可能出现不能到达@位置的情况,因此ans数组初始化时不能为0;

#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#define MAXN 205
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int vast[MAXN][MAXN];
int vist[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
char mp[MAXN][MAXN];
int ans1[MAXN][MAXN],ans2[MAXN][MAXN];
int n,m;
struct node
{
    int x,y;
    int sum;
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vast[x][y]&&mp[x][y]!='#')
        return 1;
    return 0;
}
void bfs1(int x,int y)
{
    queue<node>s;
    while(!s.empty()) s.pop();
    node xy,xz;
    xy.x=x;
    xy.y=y;
    xy.sum=0;
    s.push(xy);
    while(!s.empty())
    {
        xy=s.front();
        s.pop();
        if(mp[xy.x][xy.y]=='@')
        {
            ans1[xy.x][xy.y]=xy.sum;
        }
        for(int i=0;i<4;i++)
        {
            int xi=xy.x+vist[i][0];
            int yi=xy.y+vist[i][1];
            if(judge(xi,yi))
            {
                vast[xi][yi]=1;
                xz.x=xi;
                xz.y=yi;
                xz.sum=xy.sum+1;
                s.push(xz);
            }
        }
    }
    return ;
}
void bfs2(int x,int y)
{
    queue<node>s;
    while(!s.empty()) s.pop();
    node xy,xz;
    xy.x=x;
    xy.y=y;
    xy.sum=0;
    s.push(xy);
    while(!s.empty())
    {
        xy=s.front();
        s.pop();
        if(mp[xy.x][xy.y]=='@')
        {
            ans2[xy.x][xy.y]=xy.sum;
        }
        for(int i=0;i<4;i++)
        {
            int xi=xy.x+vist[i][0];
            int yi=xy.y+vist[i][1];
            if(judge(xi,yi))
            {
                vast[xi][yi]=1;
                xz.x=xi;
                xz.y=yi;
                xz.sum=xy.sum+1;
                s.push(xz);
            }
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int x1,y1,x2,y2;
        for(int i=0; i<n; i++)
        {
            scanf("%s",mp[i]);
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='Y')
                {
                    x1=i,y1=j;
                }
                else if(mp[i][j]=='M')
                {
                    x2=i,y2=j;
                }
            }
        }
        memset(ans1,inf,sizeof(ans1));//不能为0
        memset(ans2,inf,sizeof(ans2));
        memset(vast,0,sizeof(vast));
        vast[x1][y1]=1;
        bfs1(x1,y1);
        memset(vast,0,sizeof(vast));
        vast[x2][y2]=1;
        bfs2(x2,y2);
        int minn=inf;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='@')
                {
                    minn=min(minn,ans1[i][j]+ans2[i][j]);
                }
            }
        }
        printf("%d\n",minn*11);
    }
    return 0;
}








查看评论

HDU-2612-Find a way

//这题坑了我一晚上,用C++交不行,结果用C或者G++却过来,哎。。。。这题让我明白,如果用BFS,要保存到达“@”点的步数,bfs一直搜索,直到没发走才停。让后在主函数中进行输出比较。。。 AC代...
  • zyx520ytt
  • zyx520ytt
  • 2015-07-23 20:54:14
  • 305

HDU 2612 find a way

跑两遍BFS,开数组记录到达KFC步数。
  • sky_miange
  • sky_miange
  • 2015-02-06 11:05:16
  • 1020

HDU:2612 Find a way(双BFS+打表)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • zugofn
  • zugofn
  • 2016-07-30 00:48:15
  • 601

杭电 HDU ACM 2612 Find a way (简单两路广搜)

欢迎参加——每周六晚的BestCoder(有米!) Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/...
  • lsgqjh
  • lsgqjh
  • 2015-07-31 09:47:27
  • 1713

hdoj 2612 Find a way简单广搜

刚开始看到这道题居然想的是用肯德基店作为广搜的首节点, 然后TLE 后来想想要是好多家肯德基那么每一家就广搜一次,不超时就怪了 然后才发现用Y和M两遍广搜标记好距离就可以了,然后看每一家肯德基店二...
  • waterkokoro
  • waterkokoro
  • 2015-07-23 15:28:21
  • 335

杭电2612 Find a way(BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
  • u013634213
  • u013634213
  • 2014-12-29 18:22:43
  • 434

BFS基础题 杭电2612 Find a way 1252 Hike on a Graph

BFS与DFS简单对比DFS适合将所有的全都遍历一遍,就像之前的博客中的两道DFS基础题,比如最多可以走几步,所有的连通区域个数,这些都需要全部遍历完才能知道。而BFS,比如求最短的距离,可以直接宽度...
  • IBelieve2016
  • IBelieve2016
  • 2017-07-04 09:53:09
  • 256

[ACM] hdu Find a way

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
  • sr19930829
  • sr19930829
  • 2014-01-18 14:57:10
  • 2704

hdu 2612 find a way

点击打开链接 找到离两个人路程和的最小值。 可以广搜一遍,把有kfc的的路中最小值,找出来。 也可以两个bfs 求两个距离和最小。 坑爹的我之前吧距离和存一个数组,...
  • zoro_n
  • zoro_n
  • 2016-12-25 22:00:27
  • 102

HDU 2612 - Find a way

题目大意:Y和M要到同一家KCF碰面,每一步要走11分钟,’#‘为障碍物,’@‘为KCF。问两人到同一家KCF总时长最短要多久。 解题思路:与迷宫问题类似,只是要两个人走,将所有KCF位置读入结构体...
  • qq_38638213
  • qq_38638213
  • 2017-09-04 19:20:53
  • 50
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 728
    排名: 6万+