POJ 3026 Borg Maze "bfs+ min spanning Tree mst"

Source: Internet
Author: User

Borg Maze

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 12014

Accepted: 3925

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


The main idea: a figure, inside there is a has s. In the diagram, connect all the S and a number of steps that need to go. This is probably a bit too shallow to dictate here, and the following analysis will make sense:


Sample analysis: For example, the second sample connected to the top three a need two steps, connect the bottom three A also need two steps, s connected to the top three A and the bottom three A in the middle of a is two steps, then the last connection S and the right of a need 3 steps altogether is: 2+2+2+2+3=11,


Ideas: bfs+Kruskal, with the method of BFS to all the A/S are traversed once, build the edge, and then sort these edges, the minimum spanning tree algorithm can be.


Pit point: After entering N and M, the background test data has a bunch of spaces inside, so many people have no way to 1 a ....


Because my code is a bit down, so we're going to solve this problem in steps:

1. Main function contents

        memset (G,0,sizeof (g));        scanf ("%d%d", &m,&n);        Gets (fuck);        for (int i=0;i<n;i++)        {            gets (a[i]);        }        cont=0;        u=1;        for (int i=0;i<n;i++)//number A, give the node number        {for            (int j=0;j<m;j++)            {                if (a[i][j]== ' # ') g[i][j]=-1;                if (a[i][j]== ') g[i][j]=0;                if (a[i][j]== ' S ' | | a[i][j]== ' A ')                {                    g[i][j]=u;                    u++        ;        }}} for (int i=0;i<n;i++)        {for            (int j=0;j<m;j++)            {                if (g[i][j]>0)                BFS (I,j,g[i][j]); /bfs            }        }        MST ();//minimum spanning tree algorithm at the last
2. BFS:
void BFs (int x,int y,int from) {    memset (vis,0,sizeof (Vis));    Queue<zuobiao >s;    now.x=x;    now.y=y;    now.output=0;    Vis[x][y]=1;    S.push (now);    while (!s.empty ())    {        now=s.front ();        S.pop ();        for (int i=0;i<4;i++)        {            nex.x=now.x+fx[i];            Nex.y=now.y+fy[i];            nex.output=now.output+1;            if (nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0& &G[NEX.X][NEX.Y]!=-1)//If you can walk            {                vis[nex.x][nex.y]=1;                S.push (NEX);                if (g[nex.x][nex.y]>0)//If the point is also a node, build side                {                    e[cont].x=from;                    E[CONT].Y=G[NEX.X][NEX.Y];                    E[cont++].w=nex.output;}}}}    
3, the minimum spanning tree + and check the set part:
int find (int a) {    int r=a;    while (f[r]!=r)    r=f[r];    int i=a;    Int J;    while (i!=r)    {        j=f[i];        F[i]=r;        i=j;    }    return r;} void merge (int a,int b) {    int A, b;    A=find (a);    B=find (b);    if (a!=b)    f[b]=a;} int cmp (path A,path b) {    return A.W<B.W;} void MST () {    int output=0;    Sort (e,e+cont,cmp);    for (int i=1;i<=u;i++)    {        f[i]=i;    }    for (int i=0;i<cont;i++)    {        if (find (e[i].x)!=find (E[I].Y))        {            merge (E[I].X,E[I].Y);            OUTPUT+=E[I].W;        }    }    printf ("%d\n", output);}

Full AC code:

#include <stdio.h> #include <string.h> #include <algorithm> #include <queue>using namespace std ; struct path{int x,y,w;} e[500*500];struct zuobiao{int x,y,output;} Now,nex;int n,m;int cont,u;int f[500*500];char a[60][60];int g[60][60];int vis[60][60];int fx[4]={0,0,1,-1};int fy[4]=    {1,-1,0,0};void BFS (int x,int y,int from) {memset (vis,0,sizeof (VIS));    Queue<zuobiao >s;    Now.x=x;    Now.y=y;    now.output=0;    Vis[x][y]=1;    S.push (now);        while (!s.empty ()) {Now=s.front ();        S.pop ();            for (int i=0;i<4;i++) {nex.x=now.x+fx[i];            Nex.y=now.y+fy[i];            nex.output=now.output+1; if (nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&                &g[nex.x][nex.y]!=-1) {vis[nex.x][nex.y]=1;                S.push (NEX);        if (g[nex.x][nex.y]>0) {e[cont].x=from;            E[CONT].Y=G[NEX.X][NEX.Y];                E[cont++].w=nex.output;    }}}}}int find (int a) {int r=a;    while (F[R]!=R) r=f[r];    int i=a;    Int J;        while (i!=r) {j=f[i];        F[i]=r;    I=j; } return R;}    void merge (int a,int b) {int A, B;    A=find (a);    B=find (b); if (a!=b) f[b]=a;} int cmp (path A,path b) {return A.W&LT;B.W;}    void MST () {int output=0;    Sort (e,e+cont,cmp);    for (int i=1;i<=u;i++) {f[i]=i;            } for (int i=0;i<cont;i++) {if (Find (e[i].x)!=find (E[I].Y)) {merge (E[I].X,E[I].Y);        OUTPUT+=E[I].W; }} printf ("%d\n", output);}    Char Fuck[1000];int main () {int t;    scanf ("%d", &t);        while (t--) {memset (g,0,sizeof (g));        scanf ("%d%d", &m,&n);        Gets (fuck);        for (int i=0;i<n;i++) {gets (a[i]);        } cont=0;        U=1; for (int i=0;i<n;i++)//Give a number {for (int j=0;j<m;j++) {if (a[i][j]== ' # ') g[i][j]=-1;                if (a[i][j]== ') g[i][j]=0; if (a[i][j]== ' S ' | |                    a[i][j]== ' A ') {g[i][j]=u;                u++;                }}} for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {            if (g[i][j]>0) BFS (I,j,g[i][j]);    }} MST (); }}







POJ 3026 Borg Maze "bfs+ min spanning Tree 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.