其他分享
首页 > 其他分享> > Find a way

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. 

InputThe 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 
OutputFor 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

  题目大意:小Y和小M在n×m大小的地图上,寻找东西,他们相约在KFC(由'@'表示)中见面,已知地图中分布有若干个KFC,'.'为可通行的点,'#'为不可通过的地方,求他们走到最近的KFC要
多长时间,两人花费的时间分开计算并汇总,每走一步花费11分钟。
  思路:简单的搜索题,我一开始把它想复杂了,我想通过每个@到Y和M的距离,在进行比较,结果WA了好多次;然后尝试对人进行广搜,不设置终点,两人都搜一遍就行了。注意两人的初始标记位置都是
由1开始的,所以要减去2。代码如下:
  
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string.h>
#define LL long long
using namespace std;
int const max_n=201;
int n,m,sa,sb,ea,eb;
char mp[max_n][max_n];//地图信息数组
int used[max_n][max_n];//标记数组
int time[max_n][max_n];//这里主要是为了记录人到KFC花费的时间
int dd[4][2]={1,0,-1,0,0,1,0,-1};
struct test{
    int x,y;
};
queue<test>p;
bool judge(int x,int y){return x>=0&&x<n&&y>=0&&y<m;}//判断数组下标是否越界
int bfs()
{
    memset(time,0,sizeof(time));
    memset(used,0,sizeof(used));
    int tim=999999;//初始一个较大的时间值 
    test a;
    a.x=sa,a.y=sb;
    used[sa][sb]=1;
    p.push(a);
    while(p.size())
    {
        a=p.front();p.pop();
        if(mp[a.x][a.y]=='@')
        {
            time[a.x][a.y]+=used[a.x][a.y];//第一个人的时候直接加就好了
        }
        for(int i=0;i<4;i++)
        {
            int nx=a.x+dd[i][0];
            int ny=a.y+dd[i][1];
            if(mp[nx][ny]!='#'&&used[nx][ny]==0&&judge(nx,ny))
            {
                used[nx][ny]=used[a.x][a.y]+1;//用标记数组记录时间
                test e;
                e.x=nx,e.y=ny;
                p.push(e);
            }
        }        
    }
    memset(used,0,sizeof(used));//重置标记数组
    a.x=ea,a.y=eb;
    used[ea][eb]=1;
    p.push(a);
    while(p.size())//第二次广搜,对另一个人进行
    {
        a=p.front();p.pop();
        if(mp[a.x][a.y]=='@')
        {
            time[a.x][a.y]+=used[a.x][a.y];
            tim=min(time[a.x][a.y],tim);//把用时短的赋给tim
        }
        for(int i=0;i<4;i++)
        {
            int nx=a.x+dd[i][0];
            int ny=a.y+dd[i][1];
            if(mp[nx][ny]!='#'&&used[nx][ny]==0&&judge(nx,ny))
            {
                used[nx][ny]=used[a.x][a.y]+1;
                test e;
                e.x=nx,e.y=ny;
                p.push(e);
            }
        }        
    }
    
    return tim-2;//因为两次搜索都是从1开始的
}
int main()
{

    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int h=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)//输入并寻找Y、M所在的位置
            {
                cin>>mp[i][j];
                if(mp[i][j]=='Y')sa=i,sb=j;
                if(mp[i][j]=='M')ea=i,eb=j;
            }
        }
        LL ss=bfs();
        printf("%d\n",ss*11);
    }
    return 0;
}

 

标签:Merceki,int,max,KFC,way,include,Find,yifenfei
来源: https://www.cnblogs.com/whocarethat/p/11123092.html