zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题一 简单搜索 Find a way HDU

    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. 

    InputThe 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 OutputFor 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到他们的最短距离,然后求出最短的。
    果然超时
    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define maxn 40005
    int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[maxn],vis[205][205];
    char mapn[205][205];
    struct point{
        int x,y;
    };
    point p[maxn];
    struct node{
        int x,y,step;
    };
    void bfs(int a,int b,int c,int d,int cnt){
        node p;
        p.x = a,p.y = b,p.step = 0;
        queue<node> q;
        q.push(p);
        while(!q.empty()){
            node tmp = q.front();
            q.pop();
            if(tmp.x == c && tmp.y == d){
                sum[cnt] += tmp.step;
                return;
            }
            for(int i=0;i<4;i++){
                int xx = tmp.x + dx[i];
                int yy = tmp.y + dy[i];
                if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
                    vis[xx][yy] = 1;
                    node tp;
                    tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                    q.push(tp);
                }
            }
        }
    }
    int main()
    {
        while(cin >> n >> m){
            num = 0;
            memset(sum,0,sizeof(sum));
            if(!n || !m){
                break;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    cin >> mapn[i][j];
                    if(mapn[i][j] == '@'){
                        p[num].x = i,p[num].y = j,num++;            
                    }
                }
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(mapn[i][j] == 'Y' || mapn[i][j] == 'M'){
                        for(int k=0;k<num;k++){
                            memset(vis,0,sizeof(vis));
                            bfs(i,j,p[k].x,p[k].y,k);
                        }
                    }
                }
            }
            sort(sum,sum+num);
            cout << sum[0] * 11 << endl;
        }
        return 0;
    }

    后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    #define maxn 40005
    #define inf 0x1f1f1f1f
    int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[205][205][2],vis[205][205],z;
    char mapn[205][205];
    struct node{
        int x,y,step;
    };
    void bfs(int a,int b){
        memset(vis,0,sizeof(vis));//在这里重置vis数组
        node p;
        p.x = a,p.y = b,p.step = 0;
        vis[p.x][p.y] = 1;
        queue<node> q;
        q.push(p);
        while(!q.empty()){
            node tmp = q.front();
            q.pop();
            for(int i=0;i<4;i++){
                int xx = tmp.x + dx[i];
                int yy = tmp.y + dy[i];
                if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
                    vis[xx][yy] = 1;
                    node tp;
                    tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                    sum[tp.x][tp.y][z] = tp.step;
                    q.push(tp);
                }
            }
        }
    }
    int main()
    {
        while(cin >> n >> m){
            num = 0;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    sum[i][j][0] = inf;
                    sum[i][j][1] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
                }
            }
            if(!n || !m){
                break;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    cin >> mapn[i][j];
                }
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(mapn[i][j] == 'Y'){
                        z = 0;
                        bfs(i,j);
                    }
                    if(mapn[i][j] == 'M'){
                        z = 1;
                        bfs(i,j);
                    }
                }
            }
            int ans = inf;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(mapn[i][j] == '@'){
                        ans = min(ans,sum[i][j][0] + sum[i][j][1]);
                    }
                }
            }
            cout << ans*11 << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    php 面向对象编程实例 __construct 和 __destruct 区别
    php 数组 类对象 值传递 引用传递 区别
    WebHttpBinding.ReaderQuotas 无法设置或者无法点出来
    XP IE8 安装失败
    把XML保存为ANSI编码
    更新RDL文件中的数据集(DataSets)
    真实赛车3,FERRARI之魂不买FERRARI 599 GTO可以解锁顶点系列。
    [转]一大波“关于BUG的类型”的图片 + 一小波笑话
    sdk manager 代理,解决下载速度慢的问题
    错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib
  • 原文地址:https://www.cnblogs.com/l609929321/p/7531681.html
Copyright © 2011-2022 走看看