Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9954 | Accepted: 3341 |
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.
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的外星人,这堆人可以在任何地方分叉,人数始终够,要求最小花费。
思路:先用bfs求出所有A或S之间的最短距离,并建图。然后用求出最小生成树。
代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 200
#define inf 0x3fffffff
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int m,n;
char g[maxn][maxn];
int x[maxn],y[maxn];
int dis[maxn][maxn];
int total;
struct Node{
int x,y;
int step;
Node(){}
Node(int a,int b,int c):x(a),y(b),step(c){}
};
void bfs(int k){
bool v[maxn][maxn];
memset(v,false,sizeof(v));
queue<Node> que;
que.push(Node(x[k],y[k],0));
v[x[k]][y[k]] = true;
Node node;
while(!que.empty()){
node = que.front();
que.pop();
if(g[node.x][node.y] == 'S' || g[node.x][node.y]=='A'){
for(int i=0;i<total;i++){
if(x[i] == node.x && y[i] == node.y){
dis[k][i] = node.step;
break;
}
}
}
for(int i=0;i<4;i++){
int xx = node.x + dx[i], yy = node.y + dy[i];
if(xx>=0 && xx<m && yy>=0 && yy<n && g[xx][yy]!='#' &&!v[xx][yy]){
que.push(Node(xx,yy,node.step+1));
v[xx][yy] = true;
}
}
}
}
int prim(){
bool vis[maxn];
memset(vis,false,sizeof(vis));
vector<int> vec;
int sum = 0;
vec.push_back(0);
vis[0] = true;
while(true){
int u,v,s = inf;
for(int i=0;i<vec.size();i++){
int p = vec[i];
for(int j=0;j<total;j++){
if(!vis[j] && dis[p][j] < s){
u = p, v = j;
s = dis[p][j];
}
}
}
if(s==inf) return sum;
//printf("u:%d v:%d s:%d\n",u,v,s);
vis[v] = true;
vec.push_back(v);
sum += s;
}
}
int main(){
int N;
scanf("%d",&N);
while(N--){
scanf("%d%d\n",&n,&m);
total = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%c",&g[i][j]);
if(g[i][j]!=' ' && g[i][j]!='#'){
x[total] = i, y[total++] = j;
}
}
getchar();
}
for(int i=0;i<total;i++){
bfs(i);
/* if(i==0){
for(int j=0;j<total;j++){
printf("%d %d: %d\n",i,j,dis[i][j]);
}
}
*/
}
printf("%d\n",prim());
}
return 0;
}