3026 Borg maze BFS + prim

Source: Internet
Author: User

/*

 

Question:

There are some aliens in the rectangle where they are located. Some of them can be divided into many teams and asked what these teams need in total.

Minimum number of mobile steps

 

Analysis:

Because it can be divided into many teams, it can be seen that the Minimum Spanning Tree is obtained from the starting point (in fact, there is no starting point, MST is sure

Including the start point), so the problem is converted to the Minimum Spanning Tree. How to process the distance between a vertex and a vertex. In fact, between every two points

There must be a path, and the minimum direct distance between any two points can be obtained through BFs, so we can enumerate all points

(That is, the distance between aliens and the starting point) to another point (or the distance between aliens and the starting point. Specific implementationCode

 

*/

# Include <iostream>

# Include <cstring>

# Include <cstdio>

# Include <queue>

Using namespace STD;

# Define x 102

Int map [x] [X], ma [x] [X], DIS [X], n, m, CNT;

Int dir [4] [2] = {-}, {}, {0,-1}, {}; // direction offset

Char ch [x] [x];

Bool use [x] [X], visit [x];

Struct node // struct, which records the coordinates in the queue and the number of moved steps

{

Int X, Y, step;

};

Bool check (int x, int y) // check whether it is out of bounds

{

If (x <0 | Y <0 | x> = n | Y> = m)

Return false;

Return true;

}

Void BFS (int x, int y)

{

Memset (use, false, sizeof (use ));

Node cur;

Cur. x = X;

Cur. Y = y;

Cur. Step = 0;

Queue <node> q; // enter the queue

Q. Push (cur );

Use [x] [Y] = true;

 

Int CX, Cy, step;

Int PX = x, Py = y;

While (! Q. Empty ())

{

Cur = Q. Front (); // team lead

Q. Pop (); // Delete the team lead

 

X = cur. X;

Y = cur. Y;

Step = cur. step;

 

If (MA [x] [Y]) // note that the graph is an undirected graph, and the MA Array records the vertex subscript of the target graph under the coordinate (starting from 1)

Map [ma [x] [Y] [ma [PX] [py] = map [ma [PX] [py] [ma [x] [Y] = step;

// The map Array records the distance between the two vertices in the target graph.

 

For (INT I = 0; I <4; I ++)

{

Cx = x + dir [I] [0];

Cy = Y + dir [I] [1]; // calculates the next arrival location

If (check (CX, CY) & Ch [CX] [Cy]! = '#'&&! Use [CX] [Cy]) // no cross-border access is allowed.

{

Use [CX] [Cy] = true;

Cur. x = Cx;

Cur. Y = Cy;

Cur. Step = Step + 1; // move the distance to 1

Q. Push (cur); // join

}

}

}

}

Void prim () // calculates the minimum spanning tree. The graph is an adjacent matrix map [] [], a standard prim template.

{

Int ans = 0, K, min;

Memset (visit, false, sizeof (visit ));

 

For (INT I = 1; I <= CNT; I ++)

Dis [I] = map [I] [1];

Visit [1] = true;

For (INT I = 1; I <CNT; I ++)

{

Min = 10000000;

For (Int J = 1; j <= CNT; j ++)

If (! Visit [J] & dis [J] <min)

Min = dis [k = J];

Ans + = min;

Visit [k] = true;

For (Int J = 1; j <= CNT; j ++) // relaxation operation

If (! Visit [J])

Dis [J] = min (DIS [J], map [k] [J]);

}

Cout <ans <Endl;

}

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

Int T;

Cin> T;

While (t --)

{

CNT = 0;

Memset (MA, 0, sizeof (MA ));

Scanf ("% d", & M, & N );

For (INT I = 0; I <n; I ++)

{

Gets (CH [I]);

For (Int J = 0; j <m; j ++)

If (CH [I] [J] = 'A' | ch [I] [J] ='s ')

Ma [I] [J] = ++ CNT; // records the vertex of the target graph.

}

For (INT I = 0; I <n; I ++)

For (Int J = 0; j <m; j ++)

If (MA [I] [J]) // if it is the location or start point of an alien

BFS (I, j );

Prim (); // calculate the Shortest Path

}

 

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.