POJ3026 Borg Maze

Source: Internet
Author: User
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14165 Accepted: 4619

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 Scanning 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 alien S alive.

Sample Input

2
6 5
##### 
#A #a##
# # a#
#S  #
##### 
7 7
#####  
#AAA # #
# #    a# # # # # # # # # #AAA # # #
#####  

Sample Output

8
11

Source ————————————————————————————————————— Test instructions: In a maze where the starting point is s,a for aliens, we need to find the shortest path to connect s with all a, the shortest path to the total length
Idea: First BFS build diagram, then minimal spanning tree processing
Note: Test data more inexplicable space, GetChar () will WA, see discuss in know, IQ problem waste time

#include <iostream> #include <queue> #include <cstdio> #include <algorithm> #include <cmath
> #include <set> #include <cstring> using namespace std;

#define LL Long Long struct node {int u,v,w;} p[1000005];

struct point {int x,y,t;};
Char mp[105][105];
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

int vis[105][105];

int n,m,cnt,tot,pre[10005];

BOOL CMP (node A,node b) {return a.w<b.w;} void init () {for (int i=0; i<10005; i++) pre[i]=i;}

int fin (int x) {return Pre[x]==x?x:pre[x]=fin (pre[x]);}
    void Kruskal () {sort (p,p+cnt,cmp);
    Init ();
    int cost=0;
    int ans=0;
        for (int i=0; i<cnt; i++) {int A=fin (P[I].U);
        int B=fin (P[I].V);
            if (a!=b) {pre[a]=b;
            COST+=P[I].W;
        ans++;
        } if (ans==tot-1) {break;
}} printf ("%d\n", cost); } bool Check (int x,int y) {if (x<0| | X>=n| | y<0| | y>=m| |
    mp[x][y]== ' # ') return 0;
return 1;
    } void Build (int x,int y) {memset (vis,0,sizeof vis);
    Point F,d;
    F.x=x;
    F.y=y;
    f.t=0;
    Vis[x][y]=1;
    queue<point>q;
    Q.push (f);
        while (!q.empty ()) {F=q.front ();
        Q.pop (); if (mp[f.x][f.y]== ' A ' | | mp[f.x][f.y]== ' S ') && (f.x!=x| |
            f.y!=y)) {p[cnt].u=100*x+y;
            P[CNT].V=100*F.X+F.Y;
        p[cnt++].w=f.t;
            } for (int i=0; i<4; i++) {int xx=f.x+dir[i][0];
            int yy=f.y+dir[i][1];
                if (!vis[xx][yy]&&check (Xx,yy)) {d.x=xx;
                D.y=yy;
                d.t=f.t+1;
                Vis[xx][yy]=1;
            Q.push (d);
    }}}} int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &m,&n);
        Char temp[51];
        Gets (temp); for (int i=0; i<n;
        i++) gets (Mp[i]);
        cnt=0,tot=0; for (int i=0, i<n; i++) for (int j=0; j<m; J + +) if (mp[i][j]== ' A ' | |
        mp[i][j]== ' S ') build (i,j), tot++;
    Kruskal ();
} return 0;
 }



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.