Pku OJ 3026 Borg Maze (BFS+MST)

Source: Internet
Author: User

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12028 Accepted: 3930

Description

The Borg is a immensely powerful race of enhanced humanoids from the Delta quadrant of the Galaxy. The Borg Collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual are linked to the collective by a sophisticated subspace network that insures each member are given con Stant supervision and guidance. 

Your task is to help the Borg (yes, really) by developing a program which helps the Borg-estimate the minimal cost of s Canning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is, the beginning of the search is conducted by a large group of over individuals. Whenever an alien was assimilated, or at the beginning of the search, the group could split in and more groups (but their Consciousness is still collective.). The cost of searching a maze are definied as the total distance covered by all the groups involved in the search together. That's, if the original group walks five steps, then splits into both groups each walking three steps, the total distance Is 11=5+3+3.

Input

On the first line of input there are one integer, N <=, giving the number of test cases in the input. Each test case starts with a line containg the integers x, y such that 1 <= x, y <= 50. After this, y lines follow, each which x characters. For each character, a space "' stands for an open space, a hash mark ' # '" stands for an obstructing wall, the capital Letter "A" stand for a alien, and the capital letter "S" stands for the start of the search. The perimeter of the maze is always closed, i.e., and there is the no-to-get out from the coordinate of the ' S '. At most of aliens is present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive .

Sample Input

5##### #A #a### # a# #S  ####### 7 7##### #AAA # # # # # # # # # # # #     #AAA a##  

Sample Output

811

Source

Svenskt m?sterskap I programmering/norgesmesterskapet 2001 test instructions is asked to connect S and A at least how many steps, take the second sample example sub-a-a-a |       |---a S | | A-a-a, each horizontal line represents a step, so a total of 11 steps, see the picture is easy to see this is a minimum spanning tree, how to build a diagram is the key. First the letter number, and then go through each letter, each letter as the beginning of the BFS other letters, because it is BFS, so the resulting distance between the two letters must be the smallest, this is the weight between the two points, using this method to build a map, and then Kru algorithm for the minimum spanning tree, the code is a bit long, much detail, Debug for a long time, also is to exercise the code ability also has the most pit father of a point!!! N,m behind a lot of space, I do not know what the meaning of these spaces, but the estimated meaning is the question of human brain residue, so remember to use get to absorb the extra space to attach a group of big data, can not be able to use this group of Big Data test:
50################################################## #AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA # #                                               a## a##                                               a## a## a##                                               a## a##                                               a## a## a##                                               a## a##                                               a## a##                                           a## a## a##    a## a## a##                                               a## a##                                               a## a## a##                                               a## a##                                               a## a## a##                                               a## a##                                               a## a##                                           a## a## a##    a## a## a##                                               a## a##                                               a## a## a## a# #S a################################# ################# #答案应该输出141
The last Code
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include < Algorithm> #include <cstring>using namespace std;const int maxn = 505;int n,m;int Ans,cnt;char space[1000];int Vis[maxn][maxn];int Fx[4] = {0,0,1,-1};int Fy[4] = {1,-1,0,0};int SET[MAXN * Maxn];char a[maxn][maxn];int b[maxn][maxn];s    truct point{int x, y; int dis;};    Point now,temp,end;struct edge{int A, b; int dis;}    EDGE[MAXN * maxn];void BFS (int x,int y,int from) {memset (vis,0,sizeof (VIS));    Queue <point> q;    now.x = x;    Now.y = y;    Now.dis = 0;    Vis[x][y] = 1;    Q.push (now);        while (!q.empty ()) {temp = Q.front ();        Q.pop ();            for (int p=0;p<4;p++) {end.x = temp.x + fx[p];            End.y = temp.y + fy[p];            End.dis = Temp.dis + 1;  if (end.x >=0 && end.x < n && end.y >= 0 && end.y < m && vis[end.x][end.y] = = 0  && B[END.X][END.Y]! =-1)          {VIS[END.X][END.Y] = 1;                Q.push (end);                    if (B[end.x][end.y] > 0) {edge[ans].a = from;                    edge[ans].b = B[end.x][end.y];                Edge[ans++].dis = End.dis;  }}}}}int cmp (Edge A,edge b) {return A.dis < B.dis;}    Cmpint find (int x) {int k,j,r;    R = x;    while (r! = Set[r]) R = Set[r];    k = x;        while (k! = r) {j = set[k];        Set[k] = r;    K = J;  } return R;} and check set find + path compression void merge (int x,int y) {set[y] = x;}//and check set merge void Init () {for (int i=1;i<=cnt;i++) Set[i] = i ;}    And check the initialization of the set; int Kruskal () {init ();    int ans1 = 0;    Sort (Edge,edge + ans,cmp);        for (int i=0;i<ans;i++) {int f1 = find (EDGE[I].A);        int F2 = find (edge[i].b);        if (f1 = = F2) continue;            else {ans1 + = Edge[i].dis;        Merge (F1,F2); }    }    return ans1;}    KRU algorithm int main () {int t;    scanf ("%d", &t);        while (t--) {memset (b,0,sizeof (b));        scanf ("%d%d", &m,&n);        Gets (space);        for (int i=0;i<n;i++) {gets (a[i]);        } cnt = 1;        Ans = 0;                    for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (a[i][j] = = ' # ')                B[I][J] =-1;                if (a[i][j] = = ") B[i][j] = 0; if (a[i][j]== ' S ' | |                    a[i][j]== ' A ') {b[i][j] = cnt;                cnt++;                }}} for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {            if (B[i][j] > 0) BFS (i,j,b[i][j]);        }} int flag = Kruskal ();    printf ("%d\n", flag); } return 0;}


Pku OJ 3026 Borg Maze (BFS+MST)

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.