您的位置:首页 > 其它

Borg Maze

2016-07-14 18:46 369 查看
Borg Maze

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 12254 Accepted: 4023

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

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

题意:

从S点开始,去找每个A,在每个A处可以分开去找其他的A.#表示墙,空格可以走。

分析:

如果知道了每个A和S之间的距离,这道题就是标准的求最小生成树的题目。

那如何去找他们之间的距离了,那就要使用广搜了。还需要注意的是如何为标识每个A,所以我们按照输入的顺序给每个A编号(从1开始)。剩下的就只需要用kruskal算法求最小生成树就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>

using namespace std;

const int maxn=60;
const int maxp=110;

struct Edge
{
int u,v,w;
};

struct point
{
int x,y,step;
};

Edge E[maxp*maxp];
int map[maxn][maxn];
int v[maxn][maxn];
int p[maxp];
int n,m;
int dir[4][2]={0,-1,0,1,-1,0,1,0};

void bfs(int x,int y)
{
point now,next;
now.x=x;now.y=y;now.step=0;
queue<point> q;
q.push(now);
memset(v,0,sizeof(v));
v[x][y]=1;
int num=1;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.step=now.step+1;
if(!v[next.x][next.y] && map[next.x][next.y]>=0)
{
q.push(next);
v[next.x][next.y]=1;
if(map[next.x][next.y]>0)
{
E[m].u=map[x][y];
E[m].v=map[next.x][next.y];
E[m++].w=next.step;
num++;
if(num==n) return;
}

}
}

}
return ;
}

int cmp(Edge a,Edge b)
{
return a.w<b.w;
}

int find_(int x)
{
return p[x]==x?x:p[x]=find_(p[x]);
}

int kruskal()
{
int ans=0;
for(int i=1;i<=n;i++)
p[i]=i;
sort(E,E+m,cmp);
int num=0;
for(int i=0;i<m;i++)
{
int xx = find_(E[i].u);
int yy = find_(E[i].v);
if(xx!=yy)
{
p[xx]=yy;
ans+=E[i].w;
num++;
}
if(num>=n-1)
break;
}
return ans;
}

int main()
{
int t,r,c;
char temp[maxn];
char ch;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&c,&r);
gets(temp);
n=m=0;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
scanf("%c",&ch);
if(ch=='#')
map[i][j]=-1;
else if(ch==' ')
map[i][j]=0;
else
map[i][j]=++n;

}
getchar();
}
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
if(map[i][j]>0)
bfs(i,j);
}
}
printf("%d\n",kruskal());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: