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

    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
    BFS
    #include<iostream>
    #include<queue>
    #include<cstring>
    using namespace std;
    int m,n;
    int vis[210][210];
    char mapp[210][210];
    int dis[210][210][2];
    int dir[4][2] = {1,0,0,1,-1,0,0,-1},flag;
    struct node{
        int x;
        int y;
        int step;
    }; 
    bool judge(int x,int y)
    {
        if(x>=0&&x<m&&y>=0&&y<n&&mapp[x][y]!='#'&&vis[x][y]==0)
        return 1;
        return 0;
    }
    int BFS(int x,int y)
    {
        queue<node>q;
        node now,next;
        now.x=x;
        now.y=y;
        now.step=0;
        vis[x][y]=1;
        q.push(now);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            next.step=now.step+1;
            for(int i=0;i<4;i++)
            {
                next.x=now.x+dir[i][0];
                next.y=now.y+dir[i][1];
                if(judge(next.x,next.y))
                {
                    vis[next.x][next.y]=1;
                    if(mapp[next.x][next.y]=='@')
                    dis[next.x][next.y][flag]=next.step;
                    q.push(next);
                }
            }
        }
    }
    int main()
    {
        while(cin>>m>>n)
        {
            int min=9999999;
            for(int i=0;i<m;i++)
               for(int j=0;j<n;j++)
                   dis[i][j][0]=dis[i][j][1]=min;
            for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                cin>>mapp[i][j];
            }
            for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                if(mapp[i][j]=='Y')
                {
                    flag=0;
                    memset(vis,0,sizeof(vis));
                    BFS(i,j);
                }
                else if(mapp[i][j]=='M')
                {
                    flag=1;
                    memset(vis,0,sizeof(vis));
                    BFS(i,j);
                } 
            }
            for(int i=0;i<m;i++)
               for(int j=0;j<n;j++)
                   if(mapp[i][j]=='@' && min>dis[i][j][0]+dis[i][j][1])
                       min=dis[i][j][0]+dis[i][j][1];
               printf("%d
    ",min*11); 
        }
    }
  • 相关阅读:
    JavaScript打造很酷的图片放大效果实例代码
    【荐】CSS实现的鼠标点击小图无刷新放大图片代码
    JavaScript+CSS实现的文字幻灯切换代码
    【荐】很棒的图片友情链接带控制按钮的横向滚动代码
    jquery制作一个漂亮带渐隐效果的跑动区域
    JS打造的一款响应鼠标变化很炫的图片特效代码
    JS+CSS控制鼠标移上图片滑出文字提示代码
    Jquery+CSS实现的大气漂亮图片切换效果代码
    【荐】JS+CSS防FLASH效果带倒影的图片切换效果代码
    JavaScript限制对图片右键代码
  • 原文地址:https://www.cnblogs.com/wangmenghan/p/5565686.html
Copyright © 2011-2022 走看看