杭电搜索 2612 Find a way

Find a way

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
这道题 也不是很难    就是  双查找  找到 两人到达@的时间记录下来 最后相加作比较  求时间最少的  就是本题的解

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
    int x,y,num;
}as,x1,x2,t;
int n,m,n1,m1,k,l;
char w[201][201];
int w1[201][201];
int w2[201][201];
int dd[201][201];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
void dfs(int e)
{
    memset(dd,0,sizeof(dd));
    queue<node>q;
    if(e==0)//判断是Y  还是M   选择入队
        q.push(x1);
    else
        q.push(x2);
    while(!q.empty())
    {
        as=q.front();
        q.pop();
        dd[as.x][as.y]=1;
        for(int i=0;i<4;i++)
        {
            t.x=as.x+dir[i][0];
            t.y=as.y+dir[i][1];
            t.num=as.num+11;
            if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&dd[t.x][t.y]==0)
            {
                if(w[t.x][t.y]=='@')
                {
                    if(e==0)
                        w1[t.x][t.y]=t.num;
                    else
                        w2[t.x][t.y]=t.num;
                    q.push(t);
                }
                else if(w[t.x][t.y]=='.')
                    q.push(t);
                dd[t.x][t.y]=1;
            }
        }

    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(w,0,sizeof(w));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>w[i][j];
                if(w[i][j]=='Y')
                {
                    x1.x=i;
                    x1.y=j;
                    x1.num=0;
                }
                if(w[i][j]=='M')
                {
                    x2.x=i;
                    x2.y=j;
                    x2.num=0;
                }
            }
        }
        memset(w1,0,sizeof(w1));
        dfs(0);
        memset(w2,0,sizeof(w2));
        dfs(1);
        int ans=9999999;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(w1[i][j]!=0&&w2[i][j]!=0)//排除不是@的情况  
                ans=ans<(w1[i][j]+w2[i][j])?ans:(w1[i][j]+w2[i][j]);//比较大小 找小的
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nanfenggu/p/7900181.html