zoukankan      html  css  js  c++  java
  • hdu2612(bfs)

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6492    Accepted Submission(s): 2157


    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:  1254 1728 2102 1072 1175 
     题目意思:给定一张图,起点和终点,求经过'@'点的路径中的最短路径.
    从起点和终点开始搜,分别记录两种最短路径,有一个坑(对我来说)就是没考虑到可能有从起点或者终点不能到达某个'@'点,所以需要初始化两个距离数组为无穷大.
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    using namespace std;
    #define maxn 210
    #define inf  0x3f3f3f3f3f3f
    char Map[maxn][maxn];
    int visit[maxn][maxn];
    struct node
    {
        int x,y,dist[2]={0};
    };
    struct point
    {
        int x,y;
    };
    point ans[maxn*maxn];
    int Max;
    int dir[4][2]={0,-1,0,1,-1,0,1,0};
    int DIS1[maxn][maxn];
    int DIS2[maxn][maxn];
    node start,End;
    int n,m;
    int cnt;
    void init()
    {
        Max=inf;
        cnt=0;
        memset(visit,0,sizeof(visit));
        for(int i=0;i<maxn;i++)
            for(int j=0;j<maxn;j++)
        {
            DIS1[i][j]=inf; DIS2[i][j]=inf;
        }
    }
    void bfs(node Point,int kk,int CNT)
    {
         queue <node>  que;
          node aa;
          que.push(Point);
          visit[Point.x][Point.y]=1;
        while(!que.empty())
        {
           aa=que.front();
           que.pop();
           if(Map[aa.x][aa.y]=='@')
           {
               if(kk==0)
               {
                   DIS1[aa.x][aa.y]=aa.dist[0];
                 //  printf("start %d %d %d
    ",aa.x,aa.y,DIS1[aa.x][aa.y]);
               }
               if(kk==1)
               {
                   DIS2[aa.x][aa.y]=aa.dist[1];
                 //  printf("End %d %d %d
    ",aa.x,aa.y,DIS2[aa.x][aa.y]);
               }
               CNT--;
               if(CNT==0)
                 return ;
    
           }
           for(int i=0;i<4;i++)
           {
                node bb;
               int next_x=aa.x+dir[i][0];
               int next_y=aa.y+dir[i][1];
               if(1<=next_x && next_x<=n && 1<=next_y && next_y<=m)
                 if( (Map[next_x][next_y]!='#') && visit[next_x][next_y]==0)
               {
                   bb.x=next_x; bb.y=next_y;
                   bb.dist[kk]=aa.dist[kk]+11;
                   visit[next_x][next_y]=1;
                   que.push(bb);
               }
           }
    
        }
    }
    int main()
    {
       // freopen("test.txt","r",stdin);
        while(~scanf("%d%d%*c",&n,&m))
        {
            init();
            for(int i=1;i<=n;i++)
            {
                scanf("%s",&(Map[i][1]));
                for(int j=1;j<=m;j++)
                {
                  if(Map[i][j]=='Y')
                  {
                      start.x=i; start.y=j;
                      start.dist[0]=0;
                  }
                  if(Map[i][j]=='M')
                  {
                      End.x=i; End.y=j;
                      End.dist[1]=0;
                  }
                  if(Map[i][j]=='@')
                  {
                      cnt++;
                      ans[cnt].x=i; ans[cnt].y=j;
                  }
                }
            }
          /*  for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                printf("%c",Map[i][j]);
                printf("
    ");
            }*/
            //cout<<cnt<<endl;
             bfs(start,0,cnt);
             memset(visit,0,sizeof(visit));
             bfs(End,1,cnt);
             for(int i=1;i<=cnt;i++)
            {
                int pp,qq;
                pp=ans[i].x;  qq=ans[i].y;
                Max=min(Max,DIS1[pp][qq]+DIS2[pp][qq]);
            }
            printf("%d
    ",Max);
           // cout<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    & 【04】 Spring中Xml属性配置的解析过程
    设计模式之模板方法设计模式
    MySQL高性能索引创建策略
    oracle用户创建及权限设置
    【已解决】关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案
    ObjectStateManager 不包含具有对“Model”类型的对象的引用的 ObjectStateEntry
    【推荐活动】脚本娃娃同城会——上海站(20130112)
    【原创】对于访问IIS元数据库失败的解决(续)
    【原创】win7 plsql里查询出来的中文信息,复制粘贴的时候出现乱码(以前从没遇到过,第一次啊)
    oracle删除用户命令和部分命令
  • 原文地址:https://www.cnblogs.com/xianbin7/p/4710189.html
Copyright © 2011-2022 走看看