Poj 3026 Borg maze

Source: Internet
Author: User

Description

The Borg is an 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 is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.

Your task is to help the Borg (yes, really) by developing a program which helps the Borg to 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 that the beginning of the search is conducting by a large group of over 100 individuals. whenever an alien is assimilated, or at the beginning of the search, the Group may split in two or more groups (but their consciousness is still collective .). the cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. that is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11 = 5 + 3 + 3.

Input

On the first line of input there is one integer, n <= 50, giving the number of test cases in the input. each test case starts with a line containg two 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 an alien, and the capital letter's ''stands for the start of the search. the perimeter of the maze is always closed, I. E ., there is no way to get out from the coordinate of the's ''. at most 100 aliens are 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

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########  

Sample output

811

The question of the Minimum Spanning Tree. The minimum spanning tree between letters.

First, find a vertex and create a graph. If you look for a vertex, traverse the graph node [I] [J], indicating that the position of the MP [I] [J] in the graph stores the nth vertex. If not, the value is 0;

Then BFS calculates the shortest path between each vertex, and prim calculates the Minimum Spanning Tree. That is, it is speechless when creating a graph. There is also a pitfall in this question, that is, there are spaces after column 6 or 5, and the character string must be read. I am still too young =. =

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <limits. h> using namespace STD; char MP [110] [110]; // Save the graph int dis [110] [110]; // The distance between each vertex in the stored graph and INT node [110] [110]; // the position where I and j are stored is the nth vertex int low [110]; int visit [110] [110]; // BFS: int vis [110]; int t, n, m, num; int Dr [4] [2] = {}, {}, {0,-1}, {-}; struct nod {int X, Y; int step;}; void BFS (INT Sx, int Sy) {memset (visit, 0, sizeof (VIS It); nod St, Ed; ST. X = SX; ST. y = sy; ST. step = 0; visit [SX] [sy] = 1; queue <nod> q; q. push (ST); While (! Q. empty () {ST = Q. front (); q. pop (); If (node [st. x] [st. y] & (St. x! = SX | st. y! = Sy) dis [node [SX] [sy] [node [st. x] [st. y] = ST. step; For (INT I = 0; I <4; I ++) {int xx = ST. X + Dr [I] [0]; int YY = ST. Y + Dr [I] [1]; If (XX <0 | XX> = n | YY <0 | YY> = m) continue; if (MP [XX] [YY]! = '#'&&! Visit [XX] [YY]) {visit [XX] [YY] = 1; ed. X = xx; ed. y = YY; ed. step = ST. step + 1; q. push (ed) ;}}} void prim () {int ans = 0, Pos; memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= num; I ++) low [I] = dis [1] [I]; vis [1] = 1; for (INT I = 1; I <= num; I ++) {pos =-1; for (Int J = 1; j <= num; j ++) {If (! Vis [J] & (Pos =-1 | low [POS]> low [J]) Pos = J;} If (Pos =-1) break; ans + = low [POS]; vis [POS] = 1; for (Int J = 1; j <= num; j ++) {If (! Vis [J] & low [J]> dis [POS] [J]) low [J] = dis [POS] [J];} printf ("% d \ n", ANS);} int main () {char STR [5]; CIN> T; while (t --) {CIN> m> N; gets (STR); memset (node, 0, sizeof (node); num = 0; For (INT I = 0; I <n; I ++) {gets (MP [I]); For (Int J = 0; j <m; j ++) {If (MP [I] [J] = 's' | MP [I] [J] = 'A ') {node [I] [J] = ++ num; // indicates the nth vertex. // cout <"I:" <I <"J: "<j <node [I] [J] <Endl ;}}for (INT I = 0; I <n; I ++) // traverse each vertex in the node and sum the Shortest Path of the other vertex {for (Int J = 0; j <m; j ++) {If (node [I] [J]) BFS (I, j) ;}} prim ();} 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.