Borg Maze
题目描述:
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找到每个A与其他A和S的距离,然后求最小生成树即可,这里我用的是Kruskal算法,所以我在求每个A到其他A和S的距离前先给每个A和S标号,在进行bfs时存边就比较方便了。
AC代码:
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
int n,m,id,pre[2500],cnt; //id表示边的条数,cnt用来计数,表示A或S的个数;
struct node
{
int from,to,w;
} e[5000005]; //存边;
char s[55][55],tmp[55]; //输入数据;
bool vis[55][55];
int index[55][55]; //标记A和S的位置;
struct maze //bfs;
{
int x,y,dis;
} p1,p2;
int dx[5]= {0,0,-1,1},dy[5]= {-1,1,0,0}; //上下左右移动;
void bfs(int x,int y,int u)
{
queue<maze>q;
p1.x=x,p1.y=y,p1.dis=0;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
q.push(p1);
while(!q.empty())
{
p2=q.front();
q.pop();
for(int i=0; i<4; i++)
{
p1.x=p2.x+dx[i];
p1.y=p2.y+dy[i];
p1.dis=p2.dis+1;
if(p1.x>=0&&p1.x<n&&p1.y>=0&&p1.y<m&&!vis[p1.x][p1.y]&&s[p1.x][p1.y]!='#')
{
//printf("s[%d][%d] %c\n",p1.x,p1.y,s[p1.x][p1.y]);
q.push(p1);
vis[p1.x][p1.y]=1;
if(s[p1.x][p1.y]=='S'||s[p1.x][p1.y]=='A')
{
//printf("index[%d][%d] %d\n",p1.x,p1.y,index[p1.x][p1.y]);
e[id++]= {u,index[p1.x][p1.y],p1.dis}; //存入符合条件的边;
}
}
}
}
//cout<<id<<endl;
}
bool cmp(node a,node b)
{
return a.w<b.w;
}
int Find(int x)
{
if(x==pre[x])
return x;
else
return pre[x]=Find(pre[x]);
}
void initial()
{
for(int i=1; i<=cnt; i++)
pre[i]=i;
}
void Kruskal()
{
initial();
sort(e,e+id,cmp);
int sum=0;
for(int i=0; i<id; i++)
{
int u=e[i].from,v=e[i].to,w=e[i].w;
int fu=Find(u),fv=Find(v);
if(fu==fv)
continue;
else
{
sum+=w;
pre[fu]=fv;
}
}
cout<<sum<<endl;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
id=0;
cnt=0;
memset(index,0,sizeof(index));
memset(e,0,sizeof(e));
scanf("%d %d",&m,&n);
gets(tmp); //亲测这里不能用getchar(),数据原因;
for(int i=0; i<n; i++)
gets(s[i]);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(s[i][j]=='A'||s[i][j]=='S')
index[i][j]=++cnt;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(s[i][j]=='A'||s[i][j]=='S')
bfs(i,j,index[i][j]);
}
}
Kruskal();
}
return 0;
}