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

Hdu 2612 Find a way(bfs)

时间:2015-01-03 09:24:56      阅读:163      评论:0      收藏:0      [点我收藏+]

标签: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): 4044    Accepted Submission(s): 1348


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   |   We have carefully selected several similar problems for you:  2717 1254 1728 2102 1072 
 

题意:
求M和Y到达同一个‘@’所需要的时间总和最小,每步需要时间11分钟。

题解:
两次BFS求M和Y到每个‘@’之间的最小距离,之后求相加的最小值。


CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
typedef long long ll;

const int INF=1000010;

using namespace std;

struct node
{
    int x,y;
    int num;//保存两人到@的最小值
    int num2;
};

vector<node>q;

queue<node>que;

int n,m;
char mp[222][222];
bool vis[222][222];
int len,Min,l;
int Mx,My,Yx,Yy;
int xx[4]= {-1,0,1,0};
int yy[4]= {0,1,0,-1};

void bfs(int x,int y)
{
    memset(vis,0,sizeof vis);
    while(que.size())
        que.pop();
    node t,tt;
    t.x=x,t.y=y;
    t.num=0;
    que.push(t);
    int s=0;
    while(que.size())
    {
        t=que.front();
        que.pop();
        if(mp[t.x][t.y]=='@')
        {
            for(int i=0; i<l; i++)//找到@
            {
                if(q[i].x==t.x&&q[i].y==t.y)
                {
                    q[i].num=min(t.num,q[i].num);
                    break;
                }
            }
        }
        for(int i=0; i<4; i++)
        {
            tt.x=t.x+xx[i];
            tt.y=t.y+yy[i];
            tt.num=t.num+1;
            if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&mp[tt.x][tt.y]!='#'&&!vis[tt.x][tt.y])
            {
                que.push(tt);
                vis[tt.x][tt.y]=1;
            }
        }
    }
}

int main()
{
    //freopen("test.in","r",stdin);
    while(cin>>n>>m)
    {
        len=0;
        q.clear();
        node a;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                cin>>mp[i][j];
                if(mp[i][j]=='@')
                {
                    a.x=i,a.y=j,a.num=INF,a.num2=0;
                    q.push_back(a);
                }
                if(mp[i][j]=='Y')
                {
                    Yx=i,Yy=j;
                }
                if(mp[i][j]=='M')
                {
                    Mx=i,My=j;
                }
            }
        l=q.size();
        bfs(Mx,My);
        for(int i=0; i<l; i++)//把num的值转给num2,num重新复制无穷大
        {
            q[i].num2=q[i].num;
            q[i].num=INF;
        }
        bfs(Yx,Yy);
        Min=INF;
        for(int i=0; i<l; i++)
        {
            if(q[i].num2+q[i].num<Min)
                Min=q[i].num+q[i].num2;
        }
        cout<<Min*11<<endl;
    }
    return 0;
}



Hdu 2612 Find a way(bfs)

标签:hdu 2612 find a way   bfs   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/42344489

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