zoukankan      html  css  js  c++  java
  • bfs(双向bfs加三维数组)

    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): 32208    Accepted Submission(s): 10316


    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   |   We have carefully selected several similar problems for you:  2717 1254 1728 2102 1072
    题意:分别从F,M出发去往某个@的最小距离。。
    new note:这个码有bug但还是过了。。。没初始化ans数组inf
    3 3
    Y#@
    .M#
    ..@
    output: 66
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    using namespace std;
    typedef long long ll ;
    char a[209][209];
    int n , m ;
    int index[2] , indey[2];
    int mx , my ;
    int vis[209][209];
    int dis[4][2] = {{1 , 0} , {-1 , 0} , {0 , -1} , {0 , 1}};
    int step1[2][209][209];
    
    struct node
    {
        int x , y , step;
        node(int x , int y , int step):x(x),y(y),step(step){}
        node(){};
    };
    
    bool check(int x , int y)
    {
        if(x <= 0 || y <= 0 || x > n || y > m)
            return false ;
        if(!vis[x][y] && a[x][y] != '#')
            return true ;
        return false ;
    }
    
    int bfs(int x , int y , int p)
    {
        queue<node>q;
        node now , next ;
        memset(vis , 0 , sizeof(vis));
        q.push(node(x , y , 0));
        vis[x][y] = 1 ;
        step1[p][x][y] = 0 ;
        while(!q.empty())
        {
            now = q.front();
            q.pop();
            for(int i = 0 ; i < 4 ; i++)
            {
                next.x = now.x + dis[i][0];
                next.y = now.y + dis[i][1];
                next.step = now.step + 1 ;
                if(check(next.x , next.y))
                {
                    vis[next.x][next.y] = 1 ;
                    step1[p][next.x][next.y] = next.step ;
                    q.push(next);
                }
            }
        }
        return 0 ;
    
    
    }
    
    
    
    int main()
    {
        while(~scanf("%d%d" , &n , &m))
        {
            getchar();
            for(int i = 1 ; i <= n ; i++)
            {
                for(int j = 1 ; j <= m ; j++)
                {
                    scanf("%c" , &a[i][j]);
                    if(a[i][j] == 'Y')
                    {
                        index[0] = i;
                        indey[0] = j;
                    }
                    else if(a[i][j] == 'M')
                    {
                        index[1] = i;
                        indey[1] = j;
                    }
                }
                getchar();
            }
            int ans = INF;
            bfs(index[0] , indey[0] , 0);
            bfs(index[1] , indey[1] , 1);
    
            for(int i = 1 ; i <= n ; i++)
            {
                for(int j = 1 ; j <= m ; j++)
                {
                    if(a[i][j] == '@')
                    {
                        ans = min(ans , step1[0][i][j] + step1[1][i][j]);
                    }
                }
            }
            printf("%d
    " , ans*11);
    
        }
    
        return 0;
    }

     比赛后码:

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    const int N = 1e7 + 5 ;
    char s[209][209];
    int vis[209][209];
    int dir[4][2] = {{1 , 0} , {-1 , 0} , {0 , 1} , {0 , -1}};
    int n , m ;
    int ans[2][209][209];
    
    
    struct node{
        int x , y , w;
    };
    
    void bfs(int t , int x , int y)
    {
        node now , next , last;
        queue<node>q;
        now.x = x , now.y = y , now.w = 0 ;
        q.push(now);
        vis[x][y] = 1 ;
        while(!q.empty())
        {
            next = q.front();
            q.pop();
            if(s[next.x][next.y] == '@')
            {
                ans[t][next.x][next.y] = next.w ;
            }
            for(int i = 0 ; i < 4 ; i++)
            {
                int xx = next.x + dir[i][0];
                int yy = next.y + dir[i][1];
                int nw = next.w + 1 ;
                if(xx < 0 || xx >= n || yy < 0 || yy >= m)
                {
                    continue ;
                }
                if(vis[xx][yy] || s[xx][yy] == '#')
                {
                    continue ;
                }
                vis[xx][yy] = 1 ;
                last.x = xx , last.y = yy , last.w = nw ;
                q.push(last);
            }
        }
    }
    
    int main()
    {
    
        while(~scanf("%d%d" , &n , &m))
        {
            int x , y , x1 , y1 ;
            memset(vis , 0 , sizeof(vis));
            memset(ans , INF , sizeof(ans));//没有考虑到达不了的@(但最少有一个@可达)
            //到达不了的@应该为无穷大,否则为0则答案错误
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < m ; j++)
                {
                    cin >> s[i][j] ;
                    if(s[i][j] == 'Y')
                    {
                        x = i , y = j ;
                    }
                    if(s[i][j] == 'M')
                    {
                        x1 = i , y1 = j ;
                    }
                }
            }
            bfs(0 , x , y);
            memset(vis , 0 , sizeof(vis));
            bfs(1 , x1 , y1);
            int mi = INF ;
            for(int i = 0 ; i < n ; i++)
            {
                for(int j = 0 ; j < m ; j++)
                {
                    if(s[i][j] == '@')
                    {
                        mi = min(mi , ans[0][i][j] + ans[1][i][j]);
                    }
                }
            }
            cout << mi * 11 << endl ;
    
    
        }
    
        return 0 ;
    }
  • 相关阅读:
    spark 脚本示例
    R树的应用
    将博客搬至CSDN
    select
    注册页面的验证码的实现
    web项目.注册及登陆
    eclipse web 项目中遇到的问题总结
    Apache与Tomcat
    关于MVC整理
    JDBC
  • 原文地址:https://www.cnblogs.com/nonames/p/11394967.html
Copyright © 2011-2022 走看看