Find a way

Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 3
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.
汉语

经过一年的学习,杭州的艾芬菲终于到了故乡宁波。离宁波一年,伊芬菲有很多人见面。特别是Merceki的好朋友。
Yifenfei的家在农村,但Merceki的家位于市中心。因此,伊芬菲与梅尔塞基安排在肯德基会面。宁波有很多肯德基,他们想选择一个让总时间最小的肯德基。
现在给你一张宁波地图,yifenfei和Merceki都可以花费11分钟向上,向下,向左,向右移动到相邻的道路。

输入
输入包含多个测试用例。每个测试用例包括前两个整数n,m。(2 <= N,M <= 200)。接下来是n行,每行包含m个字符。'Y’表达了yifenfei的初始位置。‘M’表达Merceki的初始位置。’#‘禁止道路; ‘’ 路。’@'KCF

产量
对于每个测试用例输出yifenfei和Merceki到达肯德基的最短总时间。你可以肯定肯定有一个肯德基可以让他们见面。

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>
char a[201][201];
int ca[201][201];
int ce[201][201][2];
const int M=0x1f1f1f1f;
int n,m,kdj,zui=9999;
int yix,yiy,Mx,My;
int z;
using namespace std;
int b[2][4]={-1,1,0,0,0,0,-1,1};
struct node
{
    int x,y,cout;
    node(int x,int y,int z):x(x),y(y),cout(z) {}构造函数
    node() {}
}u;
void bfs(int x,int y,int z)///搜索的基本模型
{
    queue<node>q;
   memset(ca,0,sizeof(ca));
   ca[x][y]=1;
   q.push(node(x,y,0));///起点入队列
   while(!q.empty())
   {
       u=q.front();
       q.pop();
    for(int i=0;i<4;i++)
   {
      int tx=u.x+b[0][i];
      int ty=u.y+b[1][i];
       if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&ca[tx][ty]==0&&a[tx][ty]!='#')
       {
           ca[tx][ty]=1;
           ce[tx][ty][z]=u.cout+1;
           q.push(node(tx,ty,u.cout+1));
       }
   }
   }
}
int main()
{
    while(cin>>n>>m)
    {
        kdj=0;
        memset(ce,M,sizeof(ce));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                 cin>>a[i][j];
                 if(a[i][j]=='Y')yix=i,yiy=j;///记录Yifenfei的位置
                 if(a[i][j]=='M')Mx=i,My=j;///记录Merceki的位置
            }
        bfs(yix,yiy,0);
        bfs(Mx,My,1);
        int minn=9999;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            if(a[i][j]=='@')
            minn=min(minn,ce[i][j][1]+ce[i][j][0]);
            cout<<minn*11<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值