Borg Maze poj3026

Source: Internet
Author: User

First of all, this question has to let me speak out. The test data is too speechless. After the input columns and rows, spaces will still be entered, so getchar () cannot be used () to process '\ n', only gets () can be used, so that space and' \ n' can be processed at one time '.

Then let's talk about the idea of this question. I first use BFS for each A or S to find the shortest distance between it and other A or S, then, create A graph with these A or S, calculate the minimum spanning tree, and then add all edge weights.

 

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Struct st
{
Int x, y, step;
} W, tmp;
St q [2, 2505];
Constint INF = 100000000;
Int map [55] [55];
Bool visit [55] [55];
Bool visit1 [105];
Int dist [105] [105];
Int dirt [4] [2] = {, 0,-, 0 };
Int d [105];
Int t, n, m, num;
 
Bool OK (int x, int y)
{
Return x> = 0 & x <n & y> = 0 & y <m & map [x] [y]! =-1;
}
 
Void BFS (int u)
{
Memset (visit, false, sizeof (visit ));
Visit [w. x] [w. y] = true;
Int front = 0, rear = 0;
Q [rear ++] = w;
While (front <rear)
{
W = q [front ++];
If (map [w. x] [w. y]> 0)
Dist [u] [map [w. x] [w. y] = w. step;
For (int I = 0; I <4; I ++)
{
Tmp. x = w. x + dirt [I] [0];
Tmp. y = w. y + dirt [I] [1];
Tmp. step = w. step + 1;
If (OK (tmp. x, tmp. y )&&! Visit [tmp. x] [tmp. y])
{
Visit [tmp. x] [tmp. y] = true;
Q [rear ++] = tmp;
}
}
}
}
 
Int main ()
{
Int I, j, k, sum;
Scanf ("% d", & t );
Char tmp [100];
While (t --)
{
Num = 0;
Scanf ("% d", & m, & n );
// Getchar ();
Gets (tmp );
For (I = 0; I <n; I ++)
{
Gets (tmp );
For (j = 0; j <m; j ++)
{
If (tmp [j] = 's' | tmp [j] = 'A ')
Map [I] [j] = ++ num;
Else if (tmp [j] = '')
Map [I] [j] = 0;
Else
Map [I] [j] =-1;
}
}
// Calculate the shortest distance of any two alien instances.
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
If (map [I] [j]> 0)
{
W. x = I; w. y = j; w. step = 0;
BFS (map [I] [j]);
}
 
// Prim
For (I = 0; I <= num; I ++)
{
Visit1 [I] = false;
D [I] = INF;
}
D [1] = 0;
For (I = 1; I <= num; I ++)
{
K = 0;
For (j = 1; j <= num; j ++)
If (d [j] <d [k] &! Visit1 [j])
K = j;
If (k = 0)
Break;
Visit1 [k] = true;
For (j = 1; j <= num; j ++)
If (! Visit1 [j] & dist [k] [j] <d [j])
D [j] = dist [k] [j];
}
Sum = 0;
For (j = 1; j <= num; j ++)
Sum + = d [j];
Printf ("% d \ n", sum );
}
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.