zoukankan      html  css  js  c++  java
  • HDU2612 Find a way (跑两遍BFS)

    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
    注意几个地方:1.必须两个人都要访问到那个肯德基才能用它更新答案。2.Y,M,肯德基都可以经过。3.搜到肯德基以后万万不能直接continue,因为肯德基能经过。万一有
    ..Y.
    ##@#
    ##@#
    ..M.
    这样的数据就会WA!!没注意到这里卡了一下午QnQ
    #include <bits/stdc++.h>
    using namespace std;
    struct node
    {
        int x;
        int y;
        int time;
    };
    int n,m;
    char mmap[405][405];
    int ans[405][405][2];
    bool vis[405][405];
    const int INF=0x3f3f3f3f; 
    
    int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    void bfs(int p,int sx,int sy)//p为0:Y p为1:M  sx、sy为起点坐标 
    {
        queue<node>q;
        node start;
        start.x=sx,start.y=sy,start.time=0;
        q.push(start);
        vis[sx][sy]=1;
        while(q.size())
        {
            node pre;
            pre=q.front();
            q.pop();
            if(mmap[pre.x][pre.y]=='@')
            {
                ans[pre.x][pre.y][p]=pre.time*11;// p为0:Y花的时间 p为1:M花的时间 
            //    continue;千万不能写continue!!!! 
            }
            int i;
            for(i=0;i<=3;i++)
            {
                int nx=pre.x+dir[i][0];
                int ny=pre.y+dir[i][1];
                if(!vis[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<m&&mmap[nx][ny]!='#')
                {
                    node nxt;
                    nxt.x=nx;
                    nxt.y=ny;
                    nxt.time=pre.time+1;
                    q.push(nxt);
                    vis[nx][ny]=1;
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(ans,0,sizeof(ans));
            memset(mmap,0,sizeof(mmap));
            memset(vis,0,sizeof(vis));
            int i,j;
            for(i=0;i<n;i++)
            {
                scanf("%s",mmap[i]);
            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(mmap[i][j]=='Y')
                    {
                        memset(vis,0,sizeof(vis));
                        bfs(0,i,j);
                    }
                    else if(mmap[i][j]=='M')
                    { 
                        memset(vis,0,sizeof(vis));
                        bfs(1,i,j);
                    }
                }
            }    
            int out=INF;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(ans[i][j][0]!=0&&ans[i][j][1]!=0)out=min(out,ans[i][j][0]+ans[i][j][1]);//必须要两个人都能到kfc 
                }
            }    
            cout<<out<<endl;
        }    
    }

  • 相关阅读:
    word20170108逛景点 Sightseeing有用的词和句子
    c# List 分页问题
    VUE界面,this.form.xxx=1赋值后,界面效果没有变化
    SQL Server使用索引视图来实现“物化视图”
    .NET CORE 实现异步处理
    当请求接口提示网络错误Error:Network Error的时候
    SheetJS js-xlsx :js实现导出Excel模板
    增加索引分析
    聚集索引与非聚集索引的总结
    Dynamics CRM-无法识别安全令牌的颁发者,若要接受来自此颁发者的安全令牌,请将 IssuerNameRegistry 配置为返回此颁发者的有效名称
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12326353.html
Copyright © 2011-2022 走看看