[kuangbin带你飞]专题一 简单搜索 N - Find a way

N - Find a way
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 2612

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
 
分析:bfs水题,跑两次即可
code:
#include <cstdio>
#include <queue>
#include <iostream>

using namespace std;
typedef long long ll;
typedef struct{
    ll x,y;
    ll wal;
}data;
data Y,M;

ll n,m,ansY,ansM;
ll ans_[205][205],vis[205][205],dx[]={1,0,-1,0},dy[]={0,1,0,-1};
char maze[205][205];


void init()
{
    memset(ans_,0,sizeof ans_);
    memset(vis,0,sizeof vis);
    memset(maze,0,sizeof maze);
    
    ansY=0,ansM=0;
    for(int i=1;i<=n;i++)
        scanf("%s",maze[i]+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(maze[i][j]=='Y')
            {
                Y.x=i;
                Y.y=j;
                Y.wal=0;
            }
            if(maze[i][j]=='M')
            {
                M.x=i;
                M.y=j;
                M.wal=0;
            }
        }
    
}

void bfs()
{
    queue <data> q;
    q.push(Y);
    data nex;
    vis[Y.x][Y.y]=1;
    
    while(q.size())
    {
        data now=q.front();
        q.pop();
        
        ll x=now.x,y=now.y;
        if ( maze[x][y]== '@' )
            ans_[x][y]=now.wal;
        
        for(int i=0;i<4;i++)
        {
            x=now.x+dx[i],y=now.y+dy[i];
            nex.wal=now.wal+11;
            
            if( 1<=x&&x<=n&&1<=y&&y<=m)
                if(vis[x][y]==0)
                    if(maze[x][y]!='#')
                    {
                        vis[x][y]=1;
                        nex.x=x,nex.y=y;
                        q.push(nex);
                    }
        }
    }
}

void bfs_()
{
    queue <data> q;
    q.push(M);
    data nex;
    vis[M.x][M.y]=1;
    
    while(q.size())
    {
        data now=q.front();
        q.pop();
        
        ll x=now.x,y=now.y;
        if ( maze[x][y]== '@' )
            ans_[x][y]+=now.wal;
        
        for(int i=0;i<4;i++)
        {
            x=now.x+dx[i],y=now.y+dy[i];
            nex.wal=now.wal+11;
            
            if( 1<=x&&x<=n&&1<=y&&y<=m)
                if(vis[x][y]==0)
                    if(maze[x][y]!='#')
                    {
                        vis[x][y]=1;
                        nex.x=x,nex.y=y;
                        q.push(nex);
                    }
        }
    }
}

void solve()
{
    bfs();
    memset(vis,0,sizeof vis);
    bfs_();
    
    ll ans=1000000;
    
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(ans>ans_[i][j]&& ans_[i][j]) ans=ans_[i][j];
        }
    
    cout<<ans<<endl;
}

int main(void)
{
    while(cin>>n>>m)
    {
        init();
        solve();

    }
}

你可能感兴趣的:([kuangbin带你飞]专题一 简单搜索 N - Find a way)