HDU - 2612 Find a way 双起点bfs

Posted 博客就叫Molex好了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. 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
 

 

Author
yifenfei
 

 

Source
 

 

Recommend
yifenfei

 

题意:Y和M在两个不同起点,他们要到KFC集合,路上有多家KFC,问到哪家KFC能使他们的步数和最少?

思路:两边bfs,分别存取Y和M到各家KFC的步数,相加求和,枚举各KFC输出最小值。

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

char a[205][205];
int b[205][205],c[205][205];
int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

struct Node{
    int x,y,s;
}node;

int main()
{
    int n,m,yx,yy,mx,my,i,j;
    while(~scanf("%d%d",&n,&m)){
        memset(c,0,sizeof(c));
        queue<Node> q;
        for(i=0;i<n;i++){
            getchar();
            scanf("%s",a[i]);
            for(j=0;j<m;j++){
                if(a[i][j]==Y){
                    yx=i;
                    yy=j;
                }
                if(a[i][j]==M){
                    mx=i;
                    my=j;
                }
            }
        }
        memset(b,0,sizeof(b));
        b[yx][yy]=1;
        node.x=yx;
        node.y=yy;
        node.s=0;
        q.push(node);
        while(q.size()){
            for(i=0;i<4;i++){
                int tx=q.front().x+t[i][0];
                int ty=q.front().y+t[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m) continue;
                if(a[tx][ty]==#||b[tx][ty]==1) continue;
                b[tx][ty]=1;
                if(a[tx][ty]==@){
                    c[tx][ty]=q.front().s+11;
                }
                node.x=tx;
                node.y=ty;
                node.s=q.front().s+11;
                q.push(node);
            }
            q.pop();
        }
        memset(b,0,sizeof(b));
        b[mx][my]=1;
        node.x=mx;
        node.y=my;
        node.s=0;
        q.push(node);
        while(q.size()){
            for(i=0;i<4;i++){
                int tx=q.front().x+t[i][0];
                int ty=q.front().y+t[i][1];
                if(tx<0||ty<0||tx>=n||ty>=m) continue;
                if(a[tx][ty]==#||b[tx][ty]==1) continue;
                b[tx][ty]=1;
                if(a[tx][ty]==@){
                    c[tx][ty]+=q.front().s+11;
                }
                node.x=tx;
                node.y=ty;
                node.s=q.front().s+11;
                q.push(node);
            }
            q.pop();
        }
        int min=1000000000;
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(c[i][j]<min&&c[i][j]!=0) min=c[i][j];
            }
        }
        printf("%d\n",min);
    } 
    return 0;
}

 

以上是关于HDU - 2612 Find a way 双起点bfs的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2612 find a way 双BFS

HDU-2612-Find a way

hdu2612 Find a way

HDU 2612 Find a way

HDU 2612 - Find a way - [BFS]

HDU2612:Find a way