Summer Camp (1) Fourth bullet-----Find A-a-Hdu2612

Source: Internet
Author: User

Description

Pass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S.

Input

The input contains multiple test cases.
Each test case include, first, 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, both Yifenfei and Merceki to arrival one of the KFC. Sure there is always has a KFC that can let them meet.

Sample Input

4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...#

Sample Output

668866 problem Analysis: Still is BFS, but to consider the KFC is surrounded, and reset the map, and the two people can walk each other.
1#include"iostream"2#include"Queue"3 using namespacestd;4 structMa5 {6     CharZ;7     intsum;8 };9 struct PersonTen { One     inti; A     intJ; -     intTime ; - }; the intv[4][2] ={1,0,-1,0,0,-1,0,1}; -Ma maze[202][202]; -Ma maze2[202][202]; - Person fir,sec; + voidScopyintNintm) - { +       for(intI=0; i<=n+1; i++) A        for(intj=0; j<=m+1; j + +) at       { -           if(Maze2[i][j].z = ='@') -Maze[i][j].z ='@'; -           Else -maze[i][j]=Maze2[i][j]; -       } in } - voidMbegin (intNintm) to { +     inti,j; -      for(i=0; i<=n+1; i++) the       for(j=0; j<=m+1; j + +) *        if(I*j = =0|| i = = n+1|| J ==m+1) $Maze2[i][j].z ='#';Panax Notoginseng        Else -        { theCin>>maze2[i][j].z; +maze2[i][j].sum=0; Amaze[i][j].sum=0; the          if(Maze2[i][j].z = ='Y') +          { -FIR.I =i; $FIR.J =J; $          } -          if(Maze2[i][j].z = ='M') -          { theSEC.I =i; -SEC.J =J;Wuyi          } the        } - } Wu voidBFS (Person &Then ) - { AboutQueue<person>p; $ Person next; -Then.time=0; -Maze[then.i][then.j].z ='#'; - P.push (then); A     while(!p.empty ()) +    { theNext =P.front (); - P.pop (); $        for(intk=0;k<4; k++) the       { theTHEN.I = next.i+v[k][0]; theTHEN.J = next.j+v[k][1]; the         if(Maze[then.i][then.j].z! ='#') -         { inThen.time = next.time+ One; the             if(Maze[then.i][then.j].z = ='@') theMaze[then.i][then.j].sum + =Then.time; AboutMaze[then.i][then.j].z ='#'; the P.push (then); the         } the       } +    } - } the intMintime (intNintm)Bayi { the    intk=0, time; the    for(intI=1; i<=n;i++) -      for(intj=1; j<=m;j++) -       if(Maze[i][j].z = ='@') the       { the          if(Maze[i][j].sum = =0) the                           Continue; the           if(k++ = =0) -Time=maze[i][j].sum; the           if(Time >maze[i][j].sum) theTime=maze[i][j].sum; the       }94     returnTime ; the } the intMain () the {98   intn,m; About      while(cin>>n>>m) -     {101 Mbegin (n,m);102 scopy (n,m);103 BFS (FIR);104 scopy (n,m); the BFs (sec);106 scopy (n,m);107Cout<<mintime (n,m) <<Endl;108     }109     return 0; the}
View Code

Summer Camp (1) Fourth bullet-----Find A-a-Hdu2612

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.