【杭电】[2612]Find a way

Find a way

Time Limit: 3000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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

找出一个KFC使两人到它的距离之和最短 只需要把KFC坐标进行记录 然后分别以两人为起点搜索 取每个KFC的两人距离之和 其中最小的那个即为结果
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int inf=99999999;
char map[220][220];
int cnt[2][220][220];
int flag[220][220];
int tmove[4]= {1,-1,0,0};
struct node {
    int n,m;
} a[220];
int H,W;
int Yn,Ym,Mn,Mm;
int x;
void bfs(int n,int m,int who) {
    node t;
    t.n=n,t.m=m;
    queue<node>q;
    flag[n][m]=1;
    cnt[who][n][m]=0;
    q.push(t);
    while(!q.empty()) {
        t=q.front();
        for(int i=0; i<4; i++) {
            int tn=t.n+tmove[i],tm=t.m+tmove[(i+2)%4];
            if(tn>=0&&tn<H&&tm>=0&&tm<W&&!flag[tn][tm]&&map[tn][tm]!='#') {
                flag[tn][tm]=1;
                if(cnt[who][tn][tm]==inf)
                    cnt[who][tn][tm]=cnt[who][t.n][t.m]+1;
                else
                    cnt[who][tn][tm]+=cnt[who][t.n][t.m]+1;
                node temp;
                temp.n=tn,temp.m=tm;
                q.push(temp);
            }
        }
        q.pop();
    }
}
int main() {
    while(scanf("%d %d",&H,&W)!=EOF) {
        getchar();
        x=0;
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                cnt[0][i][j]=cnt[1][i][j]=inf;
                map[i][j]=getchar();
                if(map[i][j]=='@')
                    a[x].n=i,a[x++].m=j;
                else if(map[i][j]=='Y')
                    Yn=i,Ym=j;
                else if(map[i][j]=='M')
                    Mn=i,Mm=j;
            }
            getchar();
        }
        memset(flag,0,sizeof(flag));
        bfs(Yn,Ym,0);
        memset(flag,0,sizeof(flag));
        bfs(Mn,Mm,1);
        int res=inf;
        for(int i=0; i<x; i++)
            if(res>cnt[0][a[i].n][a[i].m]+cnt[1][a[i].n][a[i].m])
                res=cnt[0][a[i].n][a[i].m]+cnt[1][a[i].n][a[i].m];
        printf("%d
",res*11);
    }
    return 0;
}

题目地址:【杭电】[2612]Find a way


查看原文:http://www.boiltask.com/blog/?p=1938
原文地址:https://www.cnblogs.com/BoilTask/p/12569453.html