HDU 2612 Find A (BFS)

Source: Internet
Author: User

Find A-to

Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6251 Accepted Submission (s): 2081


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 Input4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...#

Sample Output668866

Authoryifenfei

The age of Source struggle

Recommendyifenfei Test instructions: Y and M two people agreed to meet at @, ask two people add up the shortest time is how much analysis: BFS does not need to each @ BFS, with Y and M as the starting point for BFS and then recorded to each @ time and then add the two
#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn= $+5;Const intinf=0x3f3f3f3f;intN,m,sx,sy,ex,ey,ans;intvis[maxn][maxn][2];intdir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};CharMAT[MAXN][MAXN];structnode{intx, y;} s;BOOLOkintXinty) {    if(0<=x && x<n &&0<=y && y<m)return true; Else return false;}voidBFS (intXintYintopt) {s.x=x; S.y=y; VIS[X][Y][OPT]=1; Queue<node>Q;    Q.push (s);  while(!Q.empty ())        {node Now,next; now=Q.front ();        Q.pop ();  for(intI=0;i<4; i++) {Next.x=now.x+dir[i][0]; Next.y=now.y+dir[i][1]; if(OK (NEXT.X,NEXT.Y) &&!vis[next.x][next.y][opt] && mat[next.x][next.y]!='#'&& mat[next.x][next.y]!='Y'&& mat[next.x][next.y]!='M') {vis[next.x][next.y][opt]=vis[now.x][now.y][opt]+1;            Q.push (next); }        }    }    return ;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {memset (Mat,0,sizeof(MAT));  for(intI=0; i<n;i++) {scanf ("%s", Mat[i]);  for(intj=0; j<m;j++)            {                if(mat[i][j]=='Y') SX=i,sy=J; if(mat[i][j]=='M') Ex=i,ey=J; }} memset (Vis,0,sizeof(VIS)); BFS (Sx,sy,0); BFS (Ex,ey,1); Ans=INF;  for(intI=0; i<n;i++)        {             for(intj=0; j<m;j++)            {                if(vis[i][j][0] && vis[i][j][1] && mat[i][j]=='@')                {                    intTmp= (vis[i][j][0]-1) + (vis[i][j][1]-1); if(Tmp<ans) ans=tmp; }}} printf ("%d\n", ans* One); }    return 0;}
View Code

HDU 2612 Find A (BFS)

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.