题目链接 :  http://acm.hdu.edu.cn/showproblem.php?pid=2612

 

题意:现有两个人约定好要到达KCF,两个人起始位置不一样,KCF不止一家,问你两个人都到达同一家KCF共同花费时间最少的是多少

 

 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define maxn 310
#define oo 0x3f3f3f3f
using namespace std;
char maps[maxn][maxn];
int b[maxn][maxn],v[maxn][maxn];
int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
int n, m;

struct note
{
    int x, y, step;
};

void BFS(note s)
{
    queue<note>Q;
    note now, next;
    s.step = 0;
    v[s.x][s.y]=1;

    Q.push(s);

    while(Q.size())
    {
        now = Q.front();
        Q.pop();

        if(maps[now.x][now.y]=='@')
            b[now.x][now.y] += now.step;

        for(int i=0; i<4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];

            if(next.x>=0 &&next.x<n && next.y>=0 && next.y <m && !v[next.x][next.y] && maps[next.x][next.y]!='#')
            {
                next.step = now.step+1;
                v[next.x][next.y] = 1;
                Q.push(next);
            }
        }
    }
}

int main()
{
    int  i, j;
    note s1, s2;
    while(scanf("%d %d",&n, &m)!=EOF)
    {

        for(i=0; i<n; i++)
            scanf("%s",maps[i]);

        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(maps[i][j]=='Y')
                {
                    s1.x=i;
                    s1.y=j;
                }
                if(maps[i][j]=='M')
                {
                    s2.x=i;
                    s2.y=j;
                }
            }

        memset(b,0,sizeof(b));
        memset(v,0,sizeof(v));
        BFS(s1);
        memset(v,0,sizeof(v));
        BFS(s2);

        int ans = oo;

        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(b[i][j])
                    ans = min(ans, b[i][j]);
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
}
View Code

 

posted on 2016-07-18 18:45  不忧尘世不忧心  阅读(123)  评论(0编辑  收藏  举报