您的位置:首页 > 其它

poj 3026 Borg Maze 【bfs+最小生成树】

2017-08-19 10:05 423 查看

Borg Maze

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 15066 Accepted: 4886

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###
#####


Sample Output

8
11


题意概括:

在一张地图上“A”代表外星人,“s”代表人的位置,“#”表示墙,不能走,“ ”表示可以走。当人到达外星人的位置时可以同化外星人,最少需要多少步可以将所有外星人都同化。

解题分析:

这道题其实是bfs+prim。先对“A”和“S”标号,相当于最小生成树中顶点的编号。然后用bfs建图,求出每个顶点到其他顶点的距离,然后存到二位数组中,再用prim求出最小的步数。另外,这道题数据的输入是有坑的,就是输入的x、y后面可能会有好多空格,让我WA了两次。

测试样例:

(由于空格看不到,可以用鼠标选中一下就可以看出来了)

2
6 5
######
#A#A##
# # A#
#S  ##
######
50 50
##################################################
#AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#                                               A#
#S                                              A#
##################################################


测试样例输出:

8
141


AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

struct node{
int x, y, s;
};

#define N 55
#define inf 99999999

char Map

;
int Next[4][2] = {{0,1},{0,-1},{1,0},{-1, 0}};
int e[N*N][N*N], book

, flag

, n, m, len;

void bfs(node h)
{
memset(book, 0, sizeof(book));
int i, j, tx, ty;
node head, tail;
queue<node> q;
q.push(h);
book[h.x][h.y] = 1;
while(!q.empty()){
head = q.front();
q.pop();
for(i = 0; i < 4; i++){
tx = head.x + Next[i][0<
c210
/span>];
ty = head.y + Next[i][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= m || Map[tx][ty] == '#')
continue;
if(!book[tx][ty]){
tail.x = tx;
tail.y = ty;
tail.s = head.s + 1;
q.push(tail);
book[tx][ty] = 1;
if(Map[tx][ty] == 'A' || Map[tx][ty] == 'S'){
e[flag[h.x][h.y]][flag[tx][ty]] = tail.s;
}
}
}
}
}

int prim(void)
{
int i, j, s, ans = 0, Min;
int dis[N*N], vis[N*N];
memset(vis, 0, sizeof(vis));
for(i = 1; i <= len; i++)
dis[i] = e[1][i];
vis[1] = s = 1;
while(s < len){
Min = inf;
for(i = 1; i <= len; i++)
if(!vis[i] && dis[i] < Min)
Min = dis[i], j = i;
s++;
vis[j] = 1;
ans += dis[j];
for(i = 1; i <= len; i++)
if(!vis[i] && dis[i] > e[j][i])
dis[i] = e[j][i];
}
return ans;
}

int main()
{
int i, j, T;
char str
;
node head;
scanf("%d", &T);
while(T--){
memset(flag, 0, sizeof(flag));
memset(e, 0, sizeof(e));
scanf("%d%d", &m, &n);
len = 0;
gets(str);//用来吃掉多余的空格
for(i = 0; i < n; i++){
for(j = 0;  j < m; j++){
scanf("%c", &Map[i][j]);
if(Map[i][j] == 'A' || Map[i][j] == 'S')
flag[i][j] = ++len;
}
getchar();
}
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
if(Map[i][j] == 'A' || Map[i][j] == 'S'){
head.x = i;
head.y = j;
head.s = 0;
bfs(head);
}
}
}
printf("%d\n", prim());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs 最小生成树 prim