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;
    }
  • 相关阅读:
    iOS高仿微信悬浮窗、忍者小猪游戏、音乐播放器、支付宝、今日头条布局滚动效果等源码
    iOS宇宙大战游戏、调试工具、各种动画、AR相册、相机图片编辑等源码
    android支付宝首页、蚂蚁森林效果、视频背景、校园电台、载入收缩动画等源码
    iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码
    android高仿抖音、点餐界面、天气项目、自定义view指示、爬取美女图片等源码
    Java_WebKit_ZC01
    Java_WebKit
    ZC_RemoteThread
    运行jar_测试代码
    运行jar_命令
  • 原文地址:https://www.cnblogs.com/smallhester/p/9499883.html
Copyright © 2011-2022 走看看