码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-22 16:19:55      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:c语言   bfs   

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
 

大概题意:输入地图,
                                     #      :表示墙
                                    . :表示路 
                                    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;
}




   

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

标签:c语言   bfs   

原文地址:http://blog.csdn.net/y1196645376/article/details/42079827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!