Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6634 | Accepted: 2240 |
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
借鉴自kuangbin大神:http://www.cnblogs.com/kuangbin/p/3147031.html
题意:一群人抓外星人,从s点出发,只有在s点或者在a点才能兵分多路,问人们将外星人抓完所需最短总距离。
分析:看到题目就能想到最小生成树,不过需要用bfs处理一下点与点之间的距离关系,最后prim计算一下最小生成树总花费。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const long long N=105;
const long long mod=1e9+7;
const double PI=acos(-1.0);
char temp[N][N];
int a[N][N];
int cost[N][N];
int dis[N],vis[N];
int n,y;
int t[N][N];
int mov[][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int sx,int sy) {
queue<pair<int, int> > q;
while (!q.empty()) {
q.pop();
}
memset(t, -1, sizeof(t));
t[sx][sy]=0;
q.push(make_pair(sx, sy));
while (!q.empty()) {
pair<int,int> now=q.front();
q.pop();
if(a[now.first][now.second]!=-1)
cost[a[sx][sy]][a[now.first][now.second]]=t[now.first][now.second];
for(int i=0;i<4;i++) {
int tx=now.first+mov[i][0];
int ty=now.second+mov[i][1];
if(temp[tx][ty]=='#'||t[tx][ty]!=-1)continue;
t[tx][ty]=t[now.first][now.second]+1;
q.push(make_pair(tx,ty));
}
}
}
int Prim(int n) {
int ans=0;
memset(vis, false, sizeof(vis));
vis[0]=true;
for (int i=1; i<n; i++) {
dis[i]=cost[0][i];
}
for (int i=1; i<n; i++) {
int minn=INF;
int p=-1;
for (int j=0; j<n; j++) {
if (!vis[j]&&minn>dis[j]) {
minn=dis[j];
p=j;
}
}
if (minn==INF) {
return -1;
}
ans+=minn;
vis[p]=1;
for (int j=0; j<n; j++) {
if (!vis[j]&&dis[j]>cost[p][j]) {
dis[j]=cost[p][j];
}
}
}
return ans;
}
int main() {
int t;
cin>>t;
while (t--) {
int tot=0;
cin>>y>>n;
gets(temp[0]);
memset(a, -1, sizeof(a));
for (int i=0; i<n; i++) {
gets(temp[i]);
for (int j=0; j<y; j++) {
if (temp[i][j]=='A'||temp[i][j]=='S') {
a[i][j]=tot++;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (a[i][j]!=-1) {
bfs(i, j);
}
}
}
cout<<Prim(tot)<<endl;
}
return 0;
}