Find A---hdu--2612

Source: Internet
Author: User

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find A-to

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


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

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<queue>using namespacestd;#defineINF 0x3f3f3f3f#defineN 210structnode{intx, y, step;};intN, M, A[n][n], dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};CharG[n][n];intVis[n][n];voidBFS (node S,intnum)    {node p, q; Queue<node>p;    Q.push (s); memset (Vis,0,sizeof(VIS)); VIS[S.X][S.Y]=1;  while(Q.size ()) {p=Q.front (), Q.pop (); if(g[p.x][p.y]=='@')        {            if(num==1) A[P.X][P.Y]=P.step; if(num==2) A[P.X][P.Y]+=P.step; }         for(intI=0; i<4; i++) {q.x= p.x + dir[i][0]; Q.y= P.y + dir[i][1]; Q.step= P.step +1; if(!vis[q.x][q.y] && q.x>=0&& q.x<n && q.y>=0&& q.y<m && g[q.x][q.y]!='#') {VIS[Q.X][Q.Y]=1;            Q.push (q); }        }    }}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) {        intI, J;        Node Y, M; Memset (A,0,sizeof(a));  for(i=0; i<n; i++) {scanf ("%s", G[i]);  for(j=0; j<m; J + +)            {                if(g[i][j]=='Y') y.x=i, Y.y=j, y.step=0; if(g[i][j]=='M') m.x=i, M.y=j, m.step=0; }} BFS (Y,1); BFS (M,2); intans=INF;  for(i=0; i<n; i++)         for(j=0; j<m; J + +)            if(g[i][j]=='@'&&Vis[i][j]) ans=min (ans, a[i][j]); printf ("%d\n", ans* One); }    return 0;}

Find A---hdu--2612

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.