zoukankan      html  css  js  c++  java
  • HDU

     HDU - 2612  Find a way
    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的最短路径,用两次bfs就行了,注意用return就是结束这个递归的意思
    代码:
    #include<iostream>
    #include<queue>
    #include<string.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    #define mod 1000000007
    #define INF 0x3f3f3f3f
    #define MAX 205
    int n,m;
    int sx,sy,ssx,ssy;
    int vis[MAX][MAX];
    int dis1[MAX][MAX],dis2[MAX][MAX];//记录两人到每个KFC的最短路径
    int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
    char g[MAX][MAX];
    struct mask
    {
        int x,y,step;
        mask(){}
        mask(int xx,int yy,int st)
        {
            x=xx,y=yy,step=st;
        }
    };
    bool check(int a,int b)
    {
        if(0<=a&&a<n&&0<=b&&b<m&&vis[a][b]==0&&g[a][b]!='#')
            return true;
        else return false;
    }
    queue<mask>q;
    void bfs(int x,int y,int dis[][MAX])
    {
        memset(vis,0,sizeof(vis));
        vis[x][y]=1;
        while(q.size())q.pop();
        q.push(mask(x,y,0));
        while(q.size())
        {
            mask tmp=q.front();q.pop();
            if(g[tmp.x][tmp.y]=='@')
            {
                dis[tmp.x][tmp.y]=min(dis[tmp.x][tmp.y],tmp.step);
            }
            for(int i=0;i<4;i++)
            {
                int nx=tmp.x+dx[i];
                int ny=tmp.y+dy[i];
                if(check(nx,ny))
                {
                    vis[nx][ny]=1;
                    q.push(mask(nx,ny,tmp.step+1));
                }
            }
        }
    }
    int main()
    {
        while(cin>>n>>m){
                if(n==0&&m==0) break;
        memset(dis1,INF,sizeof(dis1));
        memset(dis2,INF,sizeof(dis2));
        for(int i=0;i<n;i++)
        {
            cin>>g[i];
            for(int j=0;j<m;j++)
            {
                if(g[i][j]=='Y')
                {
                    sx=i;
                    sy=j;
                }
                if(g[i][j]=='M')
                {
                    ssx=i;
                    ssy=j;
                }
            }
        }
        //两次bfs
        bfs(sx,sy,dis1);
        bfs(ssx,ssy,dis2);
         int minn=INF;
         for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
         {
             if(g[i][j]=='@'&&dis1[i][j]!=INF&&dis2[i][j]!=INF)
                minn=min(minn,dis1[i][j]+dis2[i][j]);
         }
         cout<<minn*11<<endl;
        }
        return 0;
    }





  • 相关阅读:
    linux
    【转】三种地理参考信息模型:WMS,WFS,WCS
    Flex工程与Asp.net工程集成发布时若干注意事项
    招聘Asp.NET高级开发人员
    【转】2008'中国地理信息系统优秀工程公示
    推荐一本GIS的好书《空间数据库》
    集中推荐一些Map Projection方面资源
    收藏几篇虚拟化的文章
    征集“地图制图”WEB站点、BBS
    [转]NHibernate文章转载
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9506207.html
Copyright © 2011-2022 走看看