zoukankan      html  css  js  c++  java
  • HDU 2612 Find a way (BFS)

    Find a way

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


    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
     
    Author
    yifenfei
     
    Source
     
    Recommend
    yifenfei
     
    题意:Y和M两人约定在@处见面,问两个人加起来的最短时间是多少
    分析:BFS
    没必要对每一个@进行BFS,以Y和M为起点进行BFS然后记录下到每个@的时间再把两者进行相加即可
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=200+5;
    const int INF=0x3f3f3f3f;
    int n,m,sx,sy,ex,ey,ans;
    int vis[MAXN][MAXN][2];
    int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
    char mat[MAXN][MAXN];
    struct node
    {
        int x,y;
    }s;
    
    bool ok(int x,int y)
    {
        if(0<=x && x<n && 0<=y && y<m) return true;
        else return false;
    }
    void BFS(int x,int y,int opt)
    {
        s.x=x;
        s.y=y;
        vis[x][y][opt]=1;
        queue<node> Q;
        Q.push(s);
        while(!Q.empty())
        {
            node now,next;
            now=Q.front();Q.pop();
            for(int i=0;i<4;i++)
            {
                next.x=now.x+dir[i][0];
                next.y=now.y+dir[i][1];
                if(ok(next.x,next.y) && !vis[next.x][next.y][opt] && mat[next.x][next.y]!='#' && mat[next.x][next.y]!='Y' && mat[next.x][next.y]!='M')
                {
                    vis[next.x][next.y][opt]=vis[now.x][now.y][opt]+1;
                    Q.push(next);
                }
            }
        }
        return ;
    }
    int main()
    {
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            memset(mat,0,sizeof(mat));
            for(int i=0;i<n;i++)
            {
                scanf("%s",mat[i]);
                for(int j=0;j<m;j++)
                {
                    if(mat[i][j]=='Y')
                        sx=i,sy=j;
                    if(mat[i][j]=='M')
                        ex=i,ey=j;
                }
            }
            memset(vis,0,sizeof(vis));
            BFS(sx,sy,0);
            BFS(ex,ey,1);
            ans=INF;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(vis[i][j][0] && vis[i][j][1] && mat[i][j]=='@')
                    {
                        int tmp=(vis[i][j][0]-1)+(vis[i][j][1]-1);
                        if(tmp<ans) ans=tmp;
                    }
                }
            }
    
            printf("%d
    ",ans*11);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Ubuntu设置文件默认打开方式
    车险与费用计算(仅做参考)
    房贷计算
    PHP敏感词处理
    记一次,接口pending
    layer confirm确认框,多个按钮
    crontab vim 模式
    git指定迁出目录
    mysql树形结构
    Kubeflow实战: 入门介绍与部署实践
  • 原文地址:https://www.cnblogs.com/clliff/p/4676858.html
Copyright © 2011-2022 走看看