zoukankan      html  css  js  c++  java
  • HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

    Find a way

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


    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

    题目大意:有两个人,位置分别由Y与M代替,@是餐厅的位置,每个人移动到周围的点需要11分钟,现在求两个人到达某个餐厅的时间花费最少。

    题目思路:双向bfs遍历所有的位置,分别用两个数组作为标记,如果碰到@就保存当前花费的步数,然后两个数组对应位置相加,然后找到总步数最小的那个的值乘以11,输出就行了

    AC代码:

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    const int MAXM=205;
    char a[MAXM][MAXM];
    int n,m;
    int cx[]= {-1,1,0,0};
    int cy[]= {0,0,-1,1};
    
    struct node
    {
        int x,y;
        int flag;
        int step;
    };
    
    queue<node>Q;
    int vis1[MAXM][MAXM];
    int vis2[MAXM][MAXM];
    
    void bfs()
    {
        while(!Q.empty())
        {
            int x=Q.front().x;
            int y=Q.front().y;
            int flag=Q.front().flag;
            int cnt=Q.front().step;
            Q.pop();
            for(int i=0; i<4; i++)
            {
                int tx=x+cx[i];
                int ty=y+cy[i];
                if(flag==1)
                {
                    if(vis1[tx][ty]==-1)
                    {
                        if(a[tx][ty]=='@')
                        {
                            vis1[tx][ty]=cnt+1;
                        }
                        else
                        {
                            vis1[tx][ty]=0;
                        }
                        Q.push(node{tx,ty,1,cnt+1});
                    }
                }
                else
                {
                    if(vis2[tx][ty]==-1)
                    {
                        if(a[tx][ty]=='@')
                        {
                            vis2[tx][ty]=cnt+1;
                        }
                        else
                        {
                            vis2[tx][ty]=0;
                        }
                        Q.push(node{tx,ty,2,cnt+1});
                    }
                }
            }
        }
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        while(~scanf("%d%d",&n,&m))
        {
            mem(vis1,0);
            mem(vis2,0);
            for(int i=1; i<=n; i++)
            {
                scanf("%s",a[i]+1);
                for(int j=1; j<=m; j++)
                {
                    if(a[i][j]=='Y')
                    {
                        Q.push(node{i,j,1,0});
                        vis1[i][j]=vis2[i][j]=-1;
                    }
                    else if(a[i][j]=='M')
                    {
                        Q.push(node{i,j,2,0});
                        vis1[i][j]=vis2[i][j]=-1;
                    }
                    else if(a[i][j]=='.'||a[i][j]=='@')
                    {
                        vis1[i][j]=vis2[i][j]=-1;
                    }
                }
            }
            bfs();
    /*
            FOR(i,1,n)
            {
                FOR(j,1,m)
                {
                    printf("%3d",vis1[i][j]);
                }
                puts("");
            }
            puts("");
            FOR(i,1,n)
            {
                FOR(j,1,m)
                {
                    printf("%3d",vis2[i][j]);
                }
                puts("");
            }
    */
            int ans=100000;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                     vis1[i][j]+=vis2[i][j];
                     if(vis1[i][j]>0)ans=min(ans,vis1[i][j]);
                }
            }
            cout<<ans*11<<endl;
        }
        return 0;
    }
  • 相关阅读:
    HDU-5980
    HDU-5974
    HDU-5979
    关于position的定位
    javascript学习笔记w3chool
    表单相关css技巧
    fis压缩工具的使用
    将HTML页面内容存入json数组
    中介PHP连接前台HTML与数据库MySQL
    lesscss的使用
  • 原文地址:https://www.cnblogs.com/teble/p/7243420.html
Copyright © 2011-2022 走看看