zoukankan      html  css  js  c++  java
  • hdu 2612:Find a way(经典BFS广搜题)

    Find a way

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


    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 

     
      bfs简单广搜题。入门级难度,但是因为第一次做,还是耗费了不少时间。
      题意是Y和M要去KFC一起吃饭,@为KFC的位置,可以有多个,求出他们到达KFC耗费的最短时间(1个格子耗费11分钟)。
      用两次bfs(),分别求出Y和M到各KFC的最短路,相加之后,遍历出最短的总时间。
      第一次练习广搜的题,代码写的有些费力:
     
     1 #include <iostream>
     2 #include <queue>
     3 #include <string.h>
     4 using namespace std;
     5 char a[201][201];
     6 int step[201][201];
     7 int m1[201][201];
     8 int m2[201][201];
     9 bool isw[201][201];
    10 int dx[4] = {0,1,0,-1};
    11 int dy[4] = {1,0,-1,0};
    12 int n,m;
    13 int ycurx,ycury;
    14 int mcurx,mcury;
    15 struct NODE{
    16     int x;
    17     int y;
    18 };
    19 int Min(int a,int b)
    20 {
    21     return a<b?a:b;
    22 }
    23 bool judge(int x,int y)
    24 {
    25     if( x<1 || y<1 || x>n || y>m )    //越界 
    26         return 1;
    27     if( isw[x][y] )    //走过了 
    28         return 1;
    29     if( a[x][y]=='#' )    //碰到墙 
    30         return 1;
    31     return 0;
    32 }
    33 void bfs(int curx,int cury)
    34 {
    35     queue <NODE> q;    //创建队列 
    36     NODE cur,next;
    37     cur.x = curx;
    38     cur.y = cury;
    39     q.push(cur);    //入队 
    40     memset(isw,0,sizeof(isw));
    41     isw[curx][cury] = true;
    42     step[curx][cury] = 0;
    43     while(!q.empty()){
    44         cur = q.front();
    45         q.pop();
    46         for(int i=0;i<4;i++){
    47             int nx = cur.x + dx[i];
    48             int ny = cur.y + dy[i];
    49             if( judge(nx,ny) )     
    50                 continue;
    51             step[nx][ny] = step[cur.x][cur.y] + 1;    //记录走到下一步的步数 
    52             isw[nx][ny] = true;
    53             next.x = nx;
    54             next.y = ny;
    55             q.push(next);
    56         }
    57     }
    58 }
    59 int main()
    60 {
    61     while(cin>>n>>m){
    62         for(int i=1;i<=n;i++)
    63             for(int j=1;j<=m;j++){
    64                 cin>>a[i][j];
    65                 if(a[i][j]=='Y'){    //记录Y的位置 
    66                     ycurx=i;
    67                     ycury=j;
    68                 }
    69                 else if(a[i][j]=='M'){    //记录M的位置 
    70                     mcurx=i;
    71                     mcury=j;
    72                 }
    73             }
    74         int tmin = 999999999;
    75         bfs(ycurx,ycury);    //以(ycurx,ycury)为起点开始广度优先搜索 
    76         for(int i=1;i<=n;i++)
    77             for(int j=1;j<=m;j++){
    78                 m1[i][j]=step[i][j];
    79             }
    80         bfs(mcurx,mcury);    //以(mcurx,mcury)为起点开始广搜优先搜索 
    81         for(int i=1;i<=n;i++)
    82             for(int j=1;j<=m;j++){
    83                 m2[i][j]=step[i][j];
    84             }
    85         for(int i=1;i<=n;i++)    //遍历出最短的总步数 
    86             for(int j=1;j<=m;j++){
    87                 step[i][j]=m1[i][j] + m2[i][j];
    88                 if(a[i][j]=='@'){
    89                     tmin = Min(tmin,step[i][j]);
    90                 }
    91             }
    92         cout<<tmin*11<<endl;    //输出最短总时间 
    93     }
    94     return 0;
    95 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    转载: JS 中 new 操作符
    转载: js的值,对象,原型
    php setcookie(name, value, expires, path, domain, secure) 参数详解
    转载:js数组对象操作
    转载: js数组与 json 的区别
    转载: js的Prototype属性 解释及常用方法
    行人检测程序对接景区测试人数比对数据库切换时间不准确排查
    TSINGSEE青犀视频行人检测集成票务系统读取票务系统数据库为空的问题
    EasyWasmPlayer播放视频报错Uncaught (in promise)DOMException
    名胜景区部署TSINGSEE青犀视频监控具备哪些现实意义?
  • 原文地址:https://www.cnblogs.com/yym2013/p/3521099.html
Copyright © 2011-2022 走看看