Find a way

来源:互联网 发布:网络监控系统设计说明 编辑:程序博客网 时间:2024/06/05 16:42

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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#

Sample Output

668866
题意:用了两次BFS,求Y和M到@的最小距离
#include<cstdio>#include<queue>#include<iostream>#include<cstring>using namespace std;char ch[205][205];int a[205][205], b[205][205];int dir[4][2] = {1,0,-1,0,0,1,0,-1};int n, m;struct node{    int x, y;    int sign;}s,ss, v1, f, ff,v2;void bfs1(node v){    queue <node>q;    q.push(v);    while(!q.empty())    {        f = q.front();        q.pop();        for(int i = 0; i < 4; ++i)        {            v1.x = f.x +dir[i][0];            v1.y = f.y +dir[i][1];            if(v1.x < n&&v1.x>=0&&v1.y<m&&v1.y>=0)            {                if(ch[v1.x][v1.y]!='#'&&a[v1.x][v1.y]==0)                {                    v1.sign = f.sign+1;                    a[v1.x][v1.y] = v1.sign;                    q.push(v1);                }            }        }    }}void bfs2(node vv){    queue <node>q;    q.push(vv);    while(!q.empty())    {        ff = q.front();        q.pop();        for(int i = 0; i < 4; ++i)        {            v2.x = ff.x +dir[i][0];            v2.y = ff.y +dir[i][1];            if(v2.x < n&&v2.x>=0&&v2.y<m&&v2.y>=0)            {                if(ch[v2.x][v2.y]!='#'&&b[v2.x][v2.y]==0)                {                    v2.sign = ff.sign+1;                    b[v2.x][v2.y] = v2.sign;                    q.push(v2);                }            }        }    }}int main(){    int i, j, k, t;    int yx, yy, mx, my;    while(~scanf("%d%d", &n, &m))    {        for(i = 0; i < n; ++i)        {            scanf("%s", ch[i]);        }        for(i = 0; i < n; ++i)        {            for(j = 0; j < m; ++j)            {                if(ch[i][j] == 'Y')                {                    s.x = i;                    s.y = j;                    s.sign = 1;                    memset(a, 0, sizeof(a));                    a[i][j] = 1;                    bfs1(s);                }                if(ch[i][j]=='M')                {                    ss.x = i;                    ss.y = j;                    ss.sign = 1;                    memset(b, 0, sizeof(b));                    b[i][j] = 1;                    bfs2(ss);                }            }        }        int maxn = 2147483647;        for(i = 0; i < n; ++i)        {            for(j = 0; j < m; ++j)            {                if(ch[i][j] == '@')                {                    if(a[i][j]+b[i][j]<maxn&&a[i][j]!=0&&b[i][j]!=0)                    {                        maxn = a[i][j]+b[i][j];                    }                }            }        }        printf("%d\n", (maxn-2)*11);    }    return 0;}


0 0
原创粉丝点击