HDU - 2612 Find a way(BFS)

 HDU - 2612  Find a way
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
题意:就是找到两个人去同一个KFC的最短路径,用两次bfs就行了,注意用return就是结束这个递归的意思
代码:
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
#define MAX 205
int n,m;
int sx,sy,ssx,ssy;
int vis[MAX][MAX];
int dis1[MAX][MAX],dis2[MAX][MAX];//记录两人到每个KFC的最短路径
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
char g[MAX][MAX];
struct mask
{
    int x,y,step;
    mask(){}
    mask(int xx,int yy,int st)
    {
        x=xx,y=yy,step=st;
    }
};
bool check(int a,int b)
{
    if(0<=a&&a<n&&0<=b&&b<m&&vis[a][b]==0&&g[a][b]!='#')
        return true;
    else return false;
}
queue<mask>q;
void bfs(int x,int y,int dis[][MAX])
{
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    while(q.size())q.pop();
    q.push(mask(x,y,0));
    while(q.size())
    {
        mask tmp=q.front();q.pop();
        if(g[tmp.x][tmp.y]=='@')
        {
            dis[tmp.x][tmp.y]=min(dis[tmp.x][tmp.y],tmp.step);
        }
        for(int i=0;i<4;i++)
        {
            int nx=tmp.x+dx[i];
            int ny=tmp.y+dy[i];
            if(check(nx,ny))
            {
                vis[nx][ny]=1;
                q.push(mask(nx,ny,tmp.step+1));
            }
        }
    }
}
int main()
{
    while(cin>>n>>m){
            if(n==0&&m==0) break;
    memset(dis1,INF,sizeof(dis1));
    memset(dis2,INF,sizeof(dis2));
    for(int i=0;i<n;i++)
    {
        cin>>g[i];
        for(int j=0;j<m;j++)
        {
            if(g[i][j]=='Y')
            {
                sx=i;
                sy=j;
            }
            if(g[i][j]=='M')
            {
                ssx=i;
                ssy=j;
            }
        }
    }
    //两次bfs
    bfs(sx,sy,dis1);
    bfs(ssx,ssy,dis2);
     int minn=INF;
     for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
     {
         if(g[i][j]=='@'&&dis1[i][j]!=INF&&dis2[i][j]!=INF)
            minn=min(minn,dis1[i][j]+dis2[i][j]);
     }
     cout<<minn*11<<endl;
    }
    return 0;
}

 






posted @ 2018-08-20 16:16  better46  阅读(154)  评论(0编辑  收藏  举报