Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11235 | Accepted: 3691 |
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
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001
思路:先用BFS将各个点的最小距离算出来,然后再求最小生成树
AC代码如下:
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100+5;
char xs[maxn][maxn];
int mp[maxn][maxn];
int node[maxn][maxn];
int dis[maxn],vis1[maxn],vis2[maxn][maxn];
int xx[]={1,-1,0,0};
int yy[]={0,0,1,-1};
struct Node{
int x,y;
int n;
}tmp,now;
void bfs(int x,int y,int n,int m){
queue<Node> Q;
memset(vis2,0,sizeof(vis2));
while(!Q.empty()) Q.pop();
tmp.x=x;tmp.y=y;tmp.n=0;
Q.push(tmp);
vis2[x][y]=1;
while(!Q.empty()){
now=Q.front();Q.pop();
if(node[now.x][now.y] && (now.x!=x || now.y!=y)){
mp[node[x][y]][node[now.x][now.y]]=now.n;
}
for(int i=0;i<4;i++){
int tx=now.x+xx[i],ty=now.y+yy[i];
if(tx<0 || tx>m || ty<0 || ty>n || vis2[tx][ty])
continue;
if(xs[tx][ty]!='#'){
Node ttmp;
ttmp.x=tx,ttmp.y=ty;
vis2[tx][ty]=1;
ttmp.n=now.n+1;
Q.push(ttmp);
}
}
}
}
void prim(int n){
int ans=0;
memset(dis,INF,sizeof(dis));
memset(vis1,0,sizeof(vis1));
for(int i=1;i<=n;i++){
dis[i]=mp[1][i];
}
vis1[1]=1,dis[1]=0;
for(int i=1;i<n;i++){
int minn=INF,p=0;
for(int j=1;j<=n;j++){
if(!vis1[j] && dis[j]<minn){
minn=dis[j];
p=j;
}
}
ans+=minn;vis1[p]=1;
for(int j=1;j<=n;j++){
if(!vis1[j] && dis[j]>mp[p][j]){
dis[j]=mp[p][j];
}
}
}
cout<<ans<<endl;
}
int main(){
int t;
char tt[20];
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
gets(tt);
for(int i=0;i<m;i++){
gets(xs[i]);
}
int num=0;
memset(node,0,sizeof(node));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(xs[i][j]=='S' || xs[i][j]=='A'){
node[i][j]=++num;
}
}
}
memset(mp,INF,sizeof(mp));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(node[i][j])
bfs(i,j,n,m);
}
}
prim(num);
}
return 0;
}