zoukankan      html  css  js  c++  java
  • Find a way HDU

    Find a way

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


    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
     
    题意:两个人去同一个kfc,问两个人一共走的最短路的合,kfc有多个
    题解:每个人都要一次bfs,用一个kfc去找人会超时,先把所有的kfc都找到再处理
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    #define INF 0x3f3f3f3f
    const int maxn=60000;
    typedef pair<int,int >P;
    int n,m;
    int xa,xb,ya,yb;
    char a[205][205];
    int vis[205][205];
    int d[205][205];
    int d1[205][205];
    struct fwe
    {
        int x,y;
    }list[80000];
    int dx[4]={1,0,-1,0};
    int dy[4]={0,1,0,-1};
    void bfs(int x,int y)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                d[i][j]=INF;
        queue<P>que;
        
        que.push(P(x,y));
        d[x][y]=0;
        while(que.size())
        {
            P p=que.front();
            
            que.pop();
            for(int i=0;i<4;i++)
            {
                int nx=p.first+dx[i];
                int ny=p.second+dy[i];
                if(0<=nx && nx<n && 0<=ny && ny<m && a[nx][ny]!='#' && d[nx][ny]==INF)
                {
                    que.push(P(nx,ny));
                    d[nx][ny]=d[p.first][p.second]+1;
                }
            }
            
        }
    }
    
    void bfss(int x,int y)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                d1[i][j]=INF;
        queue<P>que;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                d1[i][j]=INF;
        que.push(P(x,y));
        d1[x][y]=0;
        while(que.size())
        {
            P p=que.front();
            
            que.pop();
            for(int i=0;i<4;i++)
            {
                int nx=p.first+dx[i];
                int ny=p.second+dy[i];
                if(0<=nx && nx<n && 0<=ny && ny<m && a[nx][ny]!='#' && d1[nx][ny]==INF)
                {
                    que.push(P(nx,ny));
                    d1[nx][ny]=d1[p.first][p.second]+1;
                }
            }
            
        }
    }
    
    int main()
    {
        int min;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    cin>>a[i][j];
                    if(a[i][j]=='Y')
                    {
                        xa=i;
                        ya=j;
                    }
                    if(a[i][j]=='M')
                    {
                        xb=i;
                        yb=j;
                    }
                }
            bfs(xa,ya);
            bfss(xb,yb);
            int minn=INF;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    if(a[i][j]=='@')
                    {
                        minn=std::min(minn,d[i][j]+d1[i][j]);
                    }
                        
                }
            cout<<minn*11<<endl;
            
        }
        return 0;
    }
  • 相关阅读:
    C#模拟百度登录并到指定网站评论回帖(一)
    4张图看明白用户、权限和租户的关系
    我的微服务之路
    IT部门不应该是一个后勤部门
    一个值只有0和1的字段,到底要不要建索引?
    论程序员的自我修养
    RBAC权限管理系统数据模型
    有史以来功能最全,使用最简单的excel导入/导出工具
    一个完全平均分布的固定长度随机数发生器
    基于WCF的RESTFul WebAPI如何对传输内容实现压缩
  • 原文地址:https://www.cnblogs.com/smallhester/p/9499883.html
Copyright © 2011-2022 走看看