Poj 3026: Borg maze (BFS graph + prim + MST)

Source: Internet
Author: User

Borg maze
Time limit:1000 ms   Memory limit:65536 K
Total submissions:8250   Accepted:2762

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


Given a maze, create a minimal spanning tree in a maze to connect all points. (These points are 'A' or 'S ')
Problem: Use BFs to find the shortest path between 's' and each 'A. Then, Prim creates the Minimum Spanning Tree.

I made a very low-level mistake ..

And that disgusting space... Do not read the solution report .. I really cannot come out ..


# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> # include <algorithm> # include <queue> # include <vector> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 200; char STR [maxn] [maxn]; // string graph int map [maxn] [maxn]; // record various characters int dis [maxn] [maxn]; // graph of the distance between characters int vist [maxn] [maxn]; // used to mark the array int DX [] = {-1, 0, 0, 1} In BFS; // int dy [] = {0,-1, 1, 0}; int cas, row, Col; // Number of cases, row, column int dot ;/ /Record the number of A and s, that is, the number of nodes in the tree int CNT; // used for the end queue struct node {int X, Y; int step in BFS; // record DISTANCE} node [maxn]; void Init () // data input and initialization {scanf ("% d", & Col, & Row ); char C [51]; gets (c); // extra space used for processing Input dot = 0; memset (node, 0, sizeof (node )); for (INT I = 0; I <row; I ++) {gets (STR [I]); For (Int J = 0; j <Col; j ++) {If (STR [I] [J] = 'A' | STR [I] [J] = 's') {dot ++; map [I] [J] = dot; node [Dot]. X = I; node [Dot]. y = J;} else if (STR [I] [J] = '') map [I] [J] = 0; else if (STR [I] [J] = '#') map [I] [J] =-1 ;}} bool judge (int A, int B) // used to determine whether bool judge is bounded, or can I find it? {if (a <0 | A> = row | B <0 | B> = Col | vist [a] [B] = 1 | map [a] [B] =-1) return false; return true;} void BFS () // BFS search and establish a distance graph {node temp, next; // records the metadata of the temporary queue and the queue of the next queue element <node> q; memset (DIS, 0, sizeof (DIS); For (INT I = 1; I <= dot; I ++) // obtain the distance from S or a to other points and create a graph. {While (! Q. empty () Q. pop (); // clear memset (vist, 0, sizeof (vist) for the queue; node [I]. step = CNT = 0; vist [node [I]. x] [node [I]. y] = 1; q. push (node [I]); // printf ("% d \ n", node [I]. x, node [I]. y); While (! Q. empty () {temp = Q. front (); q. pop (); int x = temp. x; int y = temp. y; // printf ("% d \ n", x, y); If (Map [x] [Y]! = 0 & map [x] [Y]! =-1) {CNT ++; DIS [I] [map [x] [Y] = dis [map [x] [Y] [I] = temp. step; // printf ("% d \ n", DIS [I] [map [x] [Y]); If (CNT = dot) break ;} for (Int J = 0; j <4; j ++) // Four Directions {int xx = temp. X + dx [J]; int YY = temp. Y + dy [J]; If (Judge (XX, YY) {next. X = xx; next. y = YY; next. step = temp. step + 1; vist [XX] [YY] = 1; q. push (next) ;}}}} void prim () // prim algorithm {int K; int min; int lowdis [maxn]; // used to find the minimum distance int vi S [maxn]; // records whether the node enters the tree int ans = 0; // The final result memset (VIS, 0, sizeof (VIS )); for (INT I = 1; I <= dot; I ++) lowdis [I] = inf; lowdis [1] = 0; For (INT I = 1; I <= dot; I ++) {min = inf; For (Int J = 1; j <= dot; j ++) {If (! Vis [J] & lowdis [J] <min) {k = J; min = lowdis [J] ;}} ans + = min; vis [k] = 1; for (Int J = 1; j <= dot; j ++) {If (! Vis [J] & dis [k] [J] <lowdis [J]) lowdis [J] = dis [k] [J] ;}} printf ("% d \ n", ANS);} int main () {scanf ("% d", & CAS); While (CAS --) {Init (); BFS (); prim ();} return 0 ;}






Poj 3026: Borg maze (BFS graph + prim + 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.