hdu2616搜索

N - Find a way
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 2612

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

Y 和M约定在@见面,给一个城市的地图(二维),有多个@ ,可以上下左右走,每走一步花费6分钟,求两个人到达@时间总和的最小值;;

喜欢用bfs进行搜索,用两个d[][],D[][]保存路径距离Y和M到每个位置的距离,枚举@坐标,ans = min(D+d,ans);

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define INF 10000000
using namespace std;
struct node
{
    int nx,ny;
};
char str[210][210];
int d[210][210];
int D[210][210];
int x,y,ans;
int sx,sy,ex,ey,xx,yy;
int dx[4]= {1,0,-1,0};
int dy[4]= {0,1,0,-1};
queue<node>que;
void solve(node head)
{
    node p;
    que.push(head);
    d[head.nx][head.ny]=0;
    while(que.size())
    {
        node s=que.front();
        que.pop();
        ///return d[s.nz][s.nx][s.ny];
        for(int g=0; g<4; g++)
        {
            p.nx=dx[g]+s.nx;
            p.ny=dy[g]+s.ny;
            if(  str[p.nx][p.ny]!='#'&&d[p.nx][p.ny]==INF
                    &&p.nx>=0&&p.nx<x&&p.ny>=0&&p.ny<y)
            {
                ///cout<<p.nz<<" "<<p.nx<<" "<<p.ny<<endl;

                d[p.nx][p.ny]=d[s.nx][s.ny]+1;
                /// cout<< d[p.nz][p.nx][p.ny]<<endl;
                que.push(p);
            }
        }

    }
}
int main()
{
    while(scanf("%d%d",&x,&y)!=-1)
    {
        memset(str,0,sizeof(str));
        node M,Y;
        node e[40000];
        int tol=0;
        for(int j=0; j<x; j++)
        {
            scanf("%s",str[j]);
            for(int l=0; l<y; l++)
            {
                if(str[j][l]=='M')
                {
                    M.nx=j;
                    M.ny=l;
                }
                if(str[j][l]=='Y')
                {
                    Y.nx=j;
                    Y.ny=l;
                }
                if(str[j][l]=='@')
                {
                    e[tol].nx=j;
                    e[tol++].ny=l;
                }
            }
        }
           for(int i=0; i<x; i++)
                for(int j=0; j<y; j++)
                    d[i][j]=INF;

            solve(M);
             memcpy(D,d,sizeof(d));
             for(int i=0; i<x; i++)
                for(int j=0; j<y; j++)
                    d[i][j]=INF;
            solve(Y);
        int minx=INF;
        for(int i=0; i<tol; i++)
        {
        //cout<<D[e[i].nx][e[i].ny]<<endl;
            minx=min(minx,d[e[i].nx][e[i].ny]+D[e[i].nx][e[i].ny]);
        }
        printf("%d\n",minx*11);
    }
    return 0;
}


你可能感兴趣的:(hdu2616搜索)