您的位置:首页 > 其它

HDU 2612 Find a way

2017-01-20 18:39 309 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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

 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

 

[align=left]Sample Output[/align]

66
88
66

BFS两次,第一次记录第一个人到达每个‘@’的最早时间,第二次记录第二个人到达每个‘@’的最早时间,更新Min即可,代码如下:

#include<iostream>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

typedef struct node

{

    int x,y,s;

    node(int xx=0,int yy=0,int ss=0):x(xx),y(yy),s(ss){}

}node;

char Map[205][205];

int d[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

int n,m;

node Y,M;

bool r[205][205];

int st[205][205];

bool ok(int x,int y)

{

    return x>=0&&x<n&&y>=0&&y<m&&Map[x][y]!='#';

}

void BFS()

{

    node t;

    int Min=5000000;

    memset(r,false,sizeof(r));

    queue<node> q1;

    q1.push(Y);

    r[Y.x][Y.y]=true;

    while(!q1.empty())

    {

        t=q1.front(),q1.pop();

        if(Map[t.x][t.y]=='@') {st[t.x][t.y]=t.s;}

        for(int i=0;i<4;++i)

        {

            int u=t.x+d[i][0],v=t.y+d[i][1];

            if(ok(u,v)&&!r[u][v])

            {

                r[u][v]=true;

                q1.push(node(u,v,t.s+1));

            }

        }

    }

    memset(r,false,sizeof(r));

    queue<node> q2;

    q2.push(M);

    r[M.x][M.y]=true;

    while(!q2.empty())

    {

        t=q2.front(),q2.pop();

        if(Map[t.x][t.y]=='@')

        {

            st[t.x][t.y]+=t.s;

            Min=min(Min,st[t.x][t.y]);

        }

        for(int i=0;i<4;++i)

        {

            int u=t.x+d[i][0],v=t.y+d[i][1];

            if(ok(u,v)&&!r[u][v])

            {

                r[u][v]=true;

                q2.push(node(u,v,t.s+1));

            }

        }

    }

    cout<<Min*11<<endl;

    return;

}

int main()

{

    while(cin>>n>>m)

    {

        for(int i=0;i<n;++i)

        {

            cin>>Map[i];

            for(int j=0;j<m;++j)

            {

                if(Map[i][j]=='Y') {Y.x=i,Y.y=j;}

                else if(Map[i][j]=='M') {M.x=i,M.y=j;}

            }

        }

        BFS();

    }

    return 0;

}

[align=left]
[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: