Borg Maze
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
题目大意:
一个迷宫,' '代表可以到达,'#'代表不可以到达。'S'初始点。'A'是要到达的点。
输出从S开始路过所有的A点所需要的路程(重复路过只算一次)。
解题思路:
因为重复的路过某个点,路程只算一次,就相当于计算一个包含所有的'A'和'S'的最小树。
使用BFS来计算边的权值(上下左右四种情况,简单的BFS,注意初始点的dis为0),来构成完全无向图,再用Kruskal算法算出权值和即可。(注意题目数据范围,数组开小了会WA)
PS : POJ坑爹的后台,每行后面可能有坑爹的多余空格,注意处理掉即可。
Code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#define MAXN 1000
using namespace std;
struct point
{
int x,y;
} P[];
struct point q[];
int N,M,father[];
bool vis[][],v[][];
int dis[];
struct edge
{
int begin,end;
int dis;
} T[*];
void init()
{
memset(vis,,sizeof(vis));
for (int i=; i<=; i++)
father[i]=i;
}
int bfs(int a,int b)
{
int front=,rear=;
q[front]=P[a];
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
v[i][j]=vis[i][j];
dis[front]=;
while (front<rear)
{
if (q[front].x==P[b].x&&q[front].y==P[b].y) break;
int x=q[front].x,y=q[front].y;
int a[]= {x-,x+,x,x},b[]= {y,y,y+,y-};
for (int i=; i<=; i++)
if (a[i]>=&&a[i]<=M&&b[i]>=&&b[i]<=N&&!v[a[i]][b[i]])
{
q[rear].x=a[i],q[rear].y=b[i];
dis[rear]=dis[front]+;
v[a[i]][b[i]]=;
rear++;
}
front++;
}
return dis[front];
}
int find(int x)
{
if (father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
father[fx]=fy;
}
bool cmp(struct edge a,struct edge b)
{
return a.dis<b.dis;
}
int main()
{
int C;
cin>>C;
while (C--)
{
init();
cin>>N>>M;
int k=;
char ch;
while ()
{
ch=getchar();
if (ch=='\n') break;
}
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
char tmp;
scanf("%c",&tmp);
if (tmp=='#') vis[i][j]=;
else vis[i][j]=;
if (tmp=='A'||tmp=='S')
{
P[k].x=i,P[k].y=j;
k++;
}
}
while ()
{
ch=getchar();
if (ch=='\n') break;
}
}
int t=;
for (int i=; i<k; i++)
for (int j=; j<i; j++)
{
T[t].begin=i;
T[t].end=j;
T[t].dis=bfs(i,j);
t++;
}
int cnt=,sum;
sort(T+,T+t,cmp);
for (int i=; i<t; i++)
{
if (find(T[i].begin)!=find(T[i].end))
{
cnt+=T[i].dis;
join(T[i].begin,T[i].end);
sum++;
if (sum==k-) break;
}
}
printf("%d\n",cnt);
}
return ;
}