码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 2612 Find a way 双起点bfs

时间:2017-07-28 23:37:55      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:枚举   sea   yifenfei   sizeof   mis   main   otto   put   sub   

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 Y..@. .#... .#... @..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

标签:枚举   sea   yifenfei   sizeof   mis   main   otto   put   sub   

原文地址:http://www.cnblogs.com/yzm10/p/7252706.html

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