C-Language BFS (4) ___find A (Hdu 2612)

Source: Internet
Author: User

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 Output
668866
 
Approximate test instructions: Enter the map, #: Indicates the wall.: Represents a roadYM: Two person @: means KFC. Two people are going to the same KFC, but there are many KFC on the map, the shortest distance and the output. (Note: The distance per step is 11)

Idea: to two people as the benchmark point has BFS, each find the location of KFC, in a new array (ans) corresponding to the local record distance, the last to traverse the smallest of ans is the shortest distance.
#include <stdio.h> #include <string.h>char map[211][211];    Map int book[211][211];     Tagged array int ans[211][211]; Array of record distances int a[4][2]={1,0,-1,0,0,1,0,-1},m,n;struct team{int x,y,s;}    que[100000];void BFS (int startx,int starty) {int head,tail;    int tx,ty,i;    head=tail=0;    Que[tail].x=startx;    Que[tail].y=starty;    que[tail++].s=0;    Book[startx][starty]=1;            while (Head<tail) {for (i=0;i<4;i++) {tx=que[head].x+a[i][0];            TY=QUE[HEAD].Y+A[I][1]; if (tx<0| | tx>=n| | ty<0| | ty>=m| | book[tx][ty]| |            map[tx][ty]== ' # ') continue;   if (map[tx][ty]== ' @ ') ans[tx][ty]+=que[head].s+1;            Record the distance in ans.            Book[tx][ty]=1;            QUE[TAIL].X=TX;            Que[tail].y=ty;        que[tail++].s=que[head].s+1;    } head++;    }}int Main () {int i,j,min=99;        while (scanf ("%d%d", &n,&m)!=eof) {memset (ans,0,sizeof (ans)); Memset (boOk,0,sizeof (book));        for (i=0;i<n;i++) scanf ("%s", Map[i]);        int startx,starty;         for (i=0;i<n;i++) for (j=0;j<m;j++) if (map[i][j]== ' Y ') startx=i,starty=j;       BFS (Startx,starty);        Start with the first person to expand BFS.        memset (book,0,sizeof (book));         for (i=0;i<n;i++) for (j=0;j<m;j++) if (map[i][j]== ' m ') startx=i,starty=j;     BFS (Startx,starty);        Starting from the second person began to expand BFS int min=99999;                    for (i=0;i<n;i++) for (j=0;j<m;j++) if (Ans[i][j]<min&&ans[i][j])        MIN=ANS[I][J];    printf ("%d\n", 11*min); } return 0;}




C-Language BFS (4) ___find A (Hdu 2612)

Related Article

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.