C语言BFS(4)___Find a way(Hdu 2612)

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
[email protected]
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

大概题意:输入地图,

#      :表示墙

. :表示路

YM    :表示两个人

@     :表示KFC.

两个人都要去同一家KFC,但是地图上有很多KFC,输出最短的路程和.(注:每步的路程是11)

思路:以两个人为基准点先后BFS,每次找到KFC的位置后,在新开的一个数组(ans)中对应的地方记录路程,最后遍历ans中最小的便是最短路程.

#include <stdio.h>
#include <string.h>
char map[211][211];     //地图
int book[211][211];    //标记数组
int ans[211][211];     //记录距离的数组
int a[4][2]={1,0,-1,0,0,1,0,-1},m,n;
struct Team
{
    int x,y,s;
}que[100000];
void bfs(int startx,int starty)
{
    int head,tail;
    int tx,ty,i;
    head=tail=0;
    que[tail].x=startx;
    que[tail].y=starty;
    que[tail++].s=0;
    book[startx][starty]=1;
    while(head<tail)
    {
        for(i=0;i<4;i++)
        {
            tx=que[head].x+a[i][0];
            ty=que[head].y+a[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m||book[tx][ty]||map[tx][ty]=='#')
                continue;
            if(map[tx][ty]=='@')
                ans[tx][ty]+=que[head].s+1;   //在ans记录路程.
            book[tx][ty]=1;
            que[tail].x=tx;
            que[tail].y=ty;
            que[tail++].s=que[head].s+1;
        }
        head++;
    }
}
int main()
{
    int i,j,min=99;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(ans,0,sizeof(ans));
        memset(book,0,sizeof(book));
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        int startx,starty;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                if(map[i][j]=='Y')
                    startx=i,starty=j;
        bfs(startx,starty);       //从第一个人开始展开BFS.
        memset(book,0,sizeof(book));
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                if(map[i][j]=='M')
                    startx=i,starty=j;
        bfs(startx,starty);     //从第二个人开始展开BFS
        int min=99999;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                if(ans[i][j]<min&&ans[i][j])
                    min=ans[i][j];
        printf("%d\n",11*min);
    }
    return 0;
}
时间: 2024-10-18 20:06:37

C语言BFS(4)___Find a way(Hdu 2612)的相关文章

BFS(最短路) HDU 2612 Find a way

题目传送门 1 /* 2 BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 19:36:36 7 File Name :HDOJ_2612.cpp 8 ************************************************

Problem N HDU 2612 Find a way (两次BFS求最值)

N - Find a way Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2612 Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei hav

HDU 2612 -Find a way (注意细节的BFS)

题目链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了,今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差不多,只是这题有个小坑,需要注意 1.Y不能经过M,M不能经过Y,也就是说有Y和M的格子,可以默认为是墙 2.必须是Y和M都能到达的KFC才行,只是其中一个到达不行 例如下列数据:答案既不是22 也不是 88 而是110,左下角的KFC满座条件 5 5 Y..#@ ...M. ....# ..... @.... 小

HDU 2612 Find a way bfs 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两人最大时间最小才对 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int inf=0x3fffffff; char maz[300][301]; int n,

HDU 2612 Find a way(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题目大意:给你一张n*m的图,图上有两个点Y.M,和若干个点@,找出某个点@使Y.M到这里的距离之和最小,并求出距离. 解题思路:BFS,求出Y.M到各个@的最小距离存下来,最后枚举各个@找出最小距离和即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algor

HDU - 2612 - Find a way (BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6157    Accepted Submission(s): 2052 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally

(简单) HDU 2612 Find a way,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

HDU - 2612 Find a way 双起点bfs

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16184    Accepted Submission(s): 5194 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally

HDU 2612 -Find a way (注重细节BFS)

主题链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了.今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差点儿相同.仅仅是这题有个小坑,须要注意 1.Y不能经过M.M不能经过Y.也就是说有Y和M的格子,能够默觉得是墙 2.必须是Y和M都能到达的KFC才行,仅仅是当中一个到达不行 比例如以下列数据:答案既不是22 也不是 88 而是110,左下角的KFC满座条件 5 5 Y..#@ ...M. ....# ..... @