Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11201 Accepted: 3683
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点的总路程最小是多少。
题解:
由于在任意A点和S点都可以分裂。所以先求出A,S两两之间的距离。这里可以用BFS来求。然后找出生成树即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
using namespace std;
struct Po
{
int x,y,high;
};
struct Edge
{
int from,to,v;
};
bool cmp(Edge a,Edge b){
if(a.v<b.v)
return true;
else
return false;
}
int G[500][500],cnt,e;
Po poi[10100];
int p[500][500];
int sign[100500];
Po Move[] = {-1,0,0, 1,0,0, 0,-1,0, 0,1,0};
Edge edge[10100];
void bfs(int n,int S){
queue<Po> qu;
memset(p,0,sizeof(p));
Po tem = poi[S];
qu.push(tem);
p[tem.x][tem.y] = 1;
int k = 0,i;
while(!qu.empty()) {
tem = qu.front();qu.pop();
if(G[tem.x][tem.y] == 1){
i = sign[tem.x*1000+tem.y];
edge[e].from = S;edge[e].to = i;edge[e++].v = tem.high;
k++;
}
if(k == n)
break;
f(i,0,3){
Po tem1 = tem;
tem1.high++;
tem1.x += Move[i].x;
tem1.y += Move[i].y;
if(G[tem1.x][tem1.y] != -1){
if(p[tem1.x][tem1.y] == 0){
p[tem1.x][tem1.y] = 1;
qu.push(tem1);
}
}
}
}
}
int Find(int k){
if(k == poi[k].x)
return k;
return poi[k].x = Find(poi[k].x);
}
int kruskal(int n){
sort(edge,edge+e,cmp);
int i,j,k = 0,sum = 0;
f(i,1,n){poi[i].x = i,poi[i].high = 1;}
f(i,0,e-1){
Edge tem = edge[i];
int root1 = Find(edge[i].from);
int root2 = Find(edge[i].to);
if(root1 == root2||edge[i].v == 0) continue;
k++;
sum+=edge[i].v;
if(k == n-1) return sum;
if(poi[root1].high>poi[root2].high){
poi[root2].x = root1;
poi[root1].high += poi[root2].high;
}else{
poi[root1].x = root2;
poi[root2].high += poi[root1].high;
}
}
}
int main()
{
// freopen("data.in","r",stdin);
int N;
char tem[100];
scanf("%d",&N);
while(N--) {
int n,m;
memset(sign,0,sizeof(sign));
scanf("%d%d",&m,&n);
gets(tem);
memset(G,-1,sizeof(G));
int i,j;
gets(tem);
cnt = 0;
f(i,2,n-1){
f(j,1,m){
char c = getchar();
if(c == ' ')
G[i][j] = 0;
if(c == 'S'||c == 'A'){
G[i][j] = 1;
sign[i*1000+j] = ++cnt;
poi[cnt].x = i;poi[cnt].y = j;poi[cnt].high = 0;
}
}
gets(tem);
}
gets(tem);
e = 0;
f(i,1,cnt){
bfs(cnt,i);
}
printf("%d\n",kruskal(cnt));
}
return 0;
}