Find A-to
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6673 Accepted Submission (s): 2219
Problem Descriptionpass 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.
Inputthe 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
Outputfor 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 Output668866
Authoryifenfei
The age of Source struggle
Recommendyifenfei | We have carefully selected several similar problems for you:1254 1728 1072 2579 1983 When doing this problem encountered a bug, it seems obvious wrong code A, changed to become W A, so went to see the discussion group, found that some people said y can not go m,m also can not go y, so changed after a, but later found that the code can go to others to walk can also a, the result of the end still do not know where the code is wrong, attached to the code can go. Test instructions: Y and M going to KFC to eat together, @ KFC's location, can have multiple, find out the shortest time they get to KFC (1 lattice cost 11 minutes). Using two times BFS (), respectively, find out the shortest time between Y and M to each KFC, add, and then traverse the minimum total. Attach someone else's code:
1#include <iostream>2#include <queue>3#include <string.h>4 using namespacestd;5 Chara[201][201];6 intstep[201][201];7 intm1[201][201];8 intm2[201][201];9 BOOLisw[201][201];Ten intdx[4] = {0,1,0,-1}; One intdy[4] = {1,0,-1,0}; A intn,m; - intYcurx,ycury; - intMcurx,mcury; the structnode{ - intx; - inty; - }; + intMin (intAintb) - { + returnA<b?a:b; A } at BOOLJudgeintXinty) - { - if(x<1|| y<1|| X>n | | Y>M)//Out of bounds - return 1; - if(Isw[x][y])//passed the - return 1; in if(a[x][y]=='#')//hit the wall - return 1; to return 0; + } - voidBFsintCurX,intCury) the { *Queue <NODE> q;//Create a queue $ NODE Cur,next;Panax NotoginsengCur.x =CurX; -CUR.Y =Cury; theQ.push (cur);//Queue +memset (ISW,0,sizeof(ISW)); AIsw[curx][cury] =true; theStep[curx][cury] =0; + while(!Q.empty ()) { -Cur =Q.front (); $ Q.pop (); $ for(intI=0;i<4; i++){ - intNX = Cur.x +Dx[i]; - intNY = cur.y +Dy[i]; the if(judge (Nx,ny)) - Continue;WuyiStep[nx][ny] = Step[cur.x][cur.y] +1;//record the number of steps to go to the next step theIsw[nx][ny] =true; -Next.x =NX; WuNext.y =NY; - Q.push (next); About } $ } - } - intMain () - { A while(cin>>n>>L) { + for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++){ -Cin>>A[i][j]; $ if(a[i][j]=='Y'){//record the position of Y theycurx=i; theycury=J; the } the Else if(a[i][j]=='M'){//record M's position -mcurx=i; inmcury=J; the } the } About intTmin =999999999; theBFS (Ycurx,ycury);//begin breadth-first search with (Ycurx,ycury) as a starting point the for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++){ +m1[i][j]=Step[i][j]; - } theBFS (Mcurx,mcury);//Take (Mcurx,mcury) as the starting point to start wide search firstBayi for(intI=1; i<=n;i++) the for(intj=1; j<=m;j++){ them2[i][j]=Step[i][j]; - } - for(intI=1; i<=n;i++)//traverse out the shortest total number of steps the for(intj=1; j<=m;j++){ theSTEP[I][J]=M1[I][J] +M2[i][j]; the if(a[i][j]=='@'){ theTmin =Min (Tmin,step[i][j]); - } the } thecout<<tmin* One<<endl;//output minimum total time the }94 return 0; the}
HDU 2612 Find A