HDU 2612 Find a way

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25131    Accepted Submission(s): 8195

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
 
Author
yifenfei
 
Source
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int x11,y11,x22,y22;
char map[210][210];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,ans=0x7f7f7f7f;
int vis[201][201],an[201][201][2];
void bfs(int x,int y,int opt){
    queue<int>quex,quey,step;
    memset(vis,0,sizeof(vis));
    quex.push(x);quey.push(y);
    step.push(0);vis[x][y]=1;
    while(!quex.empty()){
        int nowx=quex.front();quex.pop();
        int nowy=quey.front();quey.pop();
        int nows=step.front();step.pop();
        if(map[nowx][nowy]=='@')    an[nowx][nowy][opt]=nows;
        for(int i=0;i<4;i++){
            int cx=nowx+dx[i];
            int cy=nowy+dy[i];
            int cs=nows+11;
            if(cx>=1&&cx<=n&&cy>=1&&cy<=m&&map[cx][cy]!='#'&&!vis[cx][cy]){
                quex.push(cx);quey.push(cy);
                step.push(cs);vis[cx][cy]=1;
            }
        }
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                cin>>map[i][j];
                if(map[i][j]=='M'){ x11=i;y11=j; }
                if(map[i][j]=='Y'){ x22=i;y22=j; }
            }
        bfs(x11,y11,0);bfs(x22,y22,1);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(vis[i][j]&&map[i][j]=='@')
                    ans=min(ans,an[i][j][0]+an[i][j][1]);
        printf("%d\n",ans);
        ans=0x3f3f3f3f;
        //memset(an,0x7f,sizeof(an));
    }
} 

 

posted @ 2018-11-05 19:32  一蓑烟雨任生平  阅读(168)  评论(0编辑  收藏  举报