zoukankan      html  css  js  c++  java
  • 暑假集训(1)第四弹 -----Find a way(Hdu2612)

    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,不过要考虑kfc被包围,以及重置地图,和两人所占位都是可以互相走过的。
      1 #include "iostream"
      2 #include "queue"
      3 using namespace std;
      4 struct ma
      5 {
      6     char z;
      7     int sum;
      8 };
      9 struct person
     10 {
     11     int i;
     12     int j;
     13     int time;
     14 };
     15 int v[4][2] ={1,0,-1,0,0,-1,0,1};
     16 ma maze[202][202];
     17 ma maze2[202][202];
     18 person fir,sec;
     19 void scopy(int n,int m)
     20 {
     21      for (int i=0;i<=n+1;i++)
     22       for (int j=0;j<=m+1;j++)
     23       {
     24           if (maze2[i][j].z == '@')
     25                maze[i][j].z = '@';
     26           else
     27             maze[i][j]=maze2[i][j];
     28       }
     29 }
     30 void mbegin(int n,int m)
     31 {
     32     int i,j;
     33     for (i=0;i<=n+1;i++)
     34      for (j=0;j<=m+1;j++)
     35        if (i*j == 0 || i == n+1 || j ==m+1)
     36              maze2[i][j].z ='#';
     37        else
     38        {
     39          cin>>maze2[i][j].z;
     40          maze2[i][j].sum=0;
     41          maze[i][j].sum=0;
     42          if (maze2[i][j].z == 'Y')
     43          {
     44              fir.i = i;
     45              fir.j = j;
     46          }
     47          if (maze2[i][j].z == 'M')
     48          {
     49              sec.i =i;
     50              sec.j =j;
     51          }
     52        }
     53 }
     54 void bfs(person &then)
     55 {
     56    queue<person> p;
     57    person next;
     58    then.time=0;
     59    maze[then.i][then.j].z = '#';
     60    p.push(then);
     61    while (!p.empty())
     62    {
     63        next = p.front();
     64        p.pop();
     65       for (int k=0;k<4;k++)
     66       {
     67             then.i = next.i+v[k][0];
     68             then.j = next.j+v[k][1];
     69         if (maze[then.i][then.j].z != '#')
     70         {
     71             then.time = next.time+11;
     72             if (maze[then.i][then.j].z == '@')
     73               maze[then.i][then.j].sum += then.time;
     74             maze[then.i][then.j].z ='#';
     75             p.push(then);
     76         }
     77       }
     78    }
     79 }
     80 int mintime(int n,int m)
     81 {
     82    int k=0,time;
     83   for (int i=1;i<=n;i++)
     84     for (int j=1;j<=m;j++)
     85       if (maze[i][j].z == '@')
     86       {
     87          if (maze[i][j].sum == 0)
     88                           continue;
     89           if (k++ == 0)
     90              time=maze[i][j].sum;
     91           if (time > maze[i][j].sum)
     92               time=maze[i][j].sum;
     93       }
     94     return time;
     95 }
     96 int main()
     97 {
     98   int n,m;
     99     while (cin>>n>>m)
    100     {
    101         mbegin(n,m);
    102         scopy(n,m);
    103         bfs(fir);
    104         scopy(n,m);
    105         bfs(sec);
    106          scopy(n,m);
    107         cout<<mintime(n,m)<<endl;
    108     }
    109     return 0;
    110 }
    View Code
  • 相关阅读:
    [算法整理]树上求LCA算法合集
    线段树专题测试2017.1.21
    [数据结构]替罪羊树简介
    图论测试 2017.1.17
    bzoj 2038 A-小Z的袜子[hose]
    洛谷比赛『期末考后的休闲比赛2』
    [题解]bzoj 1861 Book 书架
    bzoj 3223 文艺平衡树
    Splay简介
    python2.7 一个莫名其妙的错误
  • 原文地址:https://www.cnblogs.com/huas-zlw/p/5676814.html
Copyright © 2011-2022 走看看