当前位置:网站首页>Borg Maze (bfs+最小生成树)

Borg Maze (bfs+最小生成树)

2022-08-10 12:52:00 51CTO



Problem 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 conducted 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


      
      
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.



Sample Output

      
      
8
11
  • 1.
  • 2.


题目大概:

在迷宫里抓住所有外星人的最佳方式。

思路:

用bfs()求出所有点(包括S和所有A)之间的最短距离,再用prim最小生成树求出最短路的距离。

这个题的输入非常坑,错了几次都是在那里,首先在输入地图前,有一个换行符,在输入数据中间后面还有空格,我用getchar()试过,是wr,所以就换了一个方法。

代码:


      
      
#include <iostream>
#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
const int INF = 0x3f3f3f3f;
using namespace std;
int n, pp;
char map[ 55][ 55];
int vis[ 55][ 55], p[ 250][ 250], a[ 250][ 250], b[ 250], vi[ 250];
int x, y, x1, y1;
int dx[ 4] ={ 0, 0, 1, - 1};
int dy[ 4] ={ 1, - 1, 0, 0};

struct poin
{
int s, d, tt;
};

int bfs( int w, int e)
{
poin t;
t. s = w; t. d = e; t. tt = 0;
queue < poin > q;
vis[ t. s][ t. d] = 1;
q. push( t);
poin w1, w2;
while( ! q. empty())
{ w1 = q. front();
q. pop();
if( map[ w1. s][ w1. d] == 'S' || map[ w1. s][ w1. d] == 'A'){
a[ p[ w][ e]][ p[ w1. s][ w1. d]] = w1. tt;
a[ p[ w1. s][ w1. d]][ p[ w][ e]] = w1. tt;}
for( int i = 0; i < 4; i ++)
{
w2. s = w1. s + dx[ i];
w2. d = w1. d + dy[ i];

if( w2. s >= 0 && w2. s < y && w2. d >= 0 && w2. d < x && map[ w2. s][ w2. d] != '#' &&! vis[ w2. s][ w2. d])
{

vis[ w2. s][ w2. d] = 1;
w2. tt = w1. tt + 1;
q. push( w2);

}

}


}

}

int prim( int qq)
{
memset( vi, 0, sizeof( vi));
for( int i = 1; i < pp; i ++)
b[ i] = a[ qq][ i];
int sum = 0;
vi[ qq] = 1;
for( int i = 2; i < pp; i ++)
{ int k = i, minn = INF;
for( int j = 1; j < pp; j ++)
{
if( ! vi[ j] && minn > b[ j])
{
minn = b[ j];
k = j;
}
}
sum += minn;
vi[ k] = 1;
for( int j = 1; j < pp; j ++)
{
if( ! vi[ j] && b[ j] > a[ k][ j])
{
b[ j] = a[ k][ j];
}
}

}
return sum;

}

int main()
{
scanf( "%d", & n);
for( int i = 1; i <= n; i ++)
{
scanf( "%d%d\n", & x, & y);
pp = 1;

for( int j = 0; j < y; j ++)
{
gets( map[ j]);
for( int k = 0; k < x; k ++)
{
if( map[ j][ k] == 'S' || map[ j][ k] == 'A'){
p[ j][ k] = pp;
pp ++;
}
}
}



for( int i = 0; i <= pp; i ++)
{
for( int j = 0; j <= pp; j ++)
{
if( i == j){ a[ i][ j] = 0;}
else a[ i][ j] = INF;
}
}

for( int i = 0; i < y; i ++)
{
for( int j = 0; j < x; j ++)
{
if( map[ i][ j] == 'S' || map[ i][ j] == 'A')
{ memset( vis, 0, sizeof( vis));
bfs( i, j);

}
}
}

cout << prim( 1) << endl;
}

return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.






原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15747246/5563379