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;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章