POJ 3026 Borg Maze

Source: Internet
Author: User

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

Test instructions: M*n's map, ' A ' stands for Aliens, ' s ' represents the origin of the search, now requires all a to be found from S, ask the minimum cost, and the movement of each step costs 1.

first, the BFS to find the minimum cost between any s or a, and then the composition of the minimum spanning tree, to get the minimum weight is the minimum cost .

#include <stdio.h>#include<queue>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x3f3f3f3f;Const intn= the;structnode{intx, y, S;};intG[n][n], Dist[n], vis[n], No[n][n], M, N, ans;///The no array holds the ordinal number of a or s in the i,j position, and ans represents the total number of S and a.intdir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };CharMap[n][n];voidInit () {intI, J;  for(i =0; i < N; i++) {Dist[i]=INF; Vis[i]=0;  for(j =0; J < N; J + +) {G[i][j]=INF; NO[I][J]=0; } G[i][i]=0; } ans=0;}voidBFS (intXintYintz) {    intI, Visit[n][n], NX, NY;    Node now, next; Queue<node>Q; memset (Visit,0,sizeof(visit)); now.x= x; Now.y = y; NOW.S =0;    Q.push (now); Visit[x][y]=1;  while(!Q.empty ()) { Now=Q.front ();        Q.pop (); if(Map[now.x][now.y] = ='S'|| MAP[NOW.X][NOW.Y] = ='A') G[z][no[now.x][now.y]]= G[no[now.x][now.y]][z] = NOW.S;///when a and s are encountered, the minimum distance from the node to which to query is saved (the minimum spanning tree is an no-map)         for(i =0; I <4; i++) {Next.x= NX = now.x + dir[i][0]; Next.y= NY = now.y + dir[i][1]; if(NX >=0&& NX < M && NY >=0&& NY < n && map[nx][ny]! ='#'&&!Visit[nx][ny]) {Visit[nx][ny]=1; Next.s= now.s+1;            Q.push (next); }        }    }}intPrim () {intI, J, Min, idex, num =0;  for(i =1; I <= ans; i++) Dist[i]= g[1][i]; vis[1] =1;///!! Easy to drop     for(i =1; i < ans; i++) {Min=INF;  for(j =1; J <= ans; J + +)        {            if(!vis[j] && Dist[j] <Min) {Min=Dist[j]; Idex=J; }} Vis[idex]=1; Num+=Min;  for(j =1; J <= ans; J + +)        {            if(Dist[j] > g[idex][j] &&!Vis[j]) dist[j]=G[idex][j]; }    }    returnnum;}intMain () {intT, I, J, num; scanf ("%d", &T);  while(t--) {Init (); scanf ("%d%d", &n, &m);  for(i =0; I < m; i++) gets (Map[i]);  for(i =0; I < m; i++)             for(j =0; J < N; J + +)                if(Map[i][j] = ='S'|| MAP[I][J] = ='A') No[i][j]= ++ans;///record the ordinal of the location node         for(i =0; I < m; i++)             for(j =0; J < N; J + +)                if(Map[i][j] = ='S'|| MAP[I][J] = ='A') BFS (i, J, No[i][j]); ///each time A and S is encountered, the BFS is searched for its distance from all other s and a.Num=Prim (); printf ("%d\n", num); }    return 0;}

POJ 3026 Borg Maze

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.