Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9071 | Accepted: 3019 |
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
用BFS搜索图形中每两个字母之间的最短距离,然后用最小生成树把他们连接起来。。。。。
后台数据真坑。。。。
如果上面第一组数据6 5 后面你加上10个空格,能过的话说名你去空格还是比较彻底的,我直接用gets函数把所有空格去掉。。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
char mp[310][310];
int lk[310][310];
struct rob
{
int x;
int y;
int step;
} rb[1010];
struct node
{
int u;
int v;
int w;
} ls[100010];
bool vis[310][310];
int cnt,num;
queue <rob> q;
int bhx[] = {0,0,1,-1};
int bhy[] = {1,-1,0,0};
int d[310];
void creat(int a,int b,int c)
{
ls[num].u = a;
ls[num].v = b;
ls[num].w = c;
num++;
}
void bfs(int s,int x,int y,int n,int m)
{
while(!q.empty())
q.pop();
memset(vis,false,sizeof(vis));
rob t,f;
t.x = x;
t.y = y;
t.step = 0;
q.push(t);
vis[x][y] = true;
while(!q.empty())
{
t = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
f.x = t.x + bhx[i];
f.y = t.y + bhy[i];
if(f.x >= 0 && f.x < n && f.y >= 0 && f.y < m && \
mp[f.x][f.y] != '#' && !vis[f.x][f.y] && mp[f.x][f.y] != '\0')
{
f.step = t.step + 1;
if(lk[f.x][f.y])
{
creat(s,lk[f.x][f.y],f.step);
}
q.push(f);
vis[f.x][f.y] = true;
}
}
}
}
bool cmp(node p,node q)
{
return p.w < q.w;
}
int nfind(int x)
{
int r = x,q;
while(x != d[x])
x = d[x];
while(d[r] != x)
{
q = d[r];
d[r] = x;
r = q;
}
return x;
}
bool lj(int x,int y)
{
int fx = nfind(x);
int fy = nfind(y);
if(fx != fy)
{
d[fx] = fy;
return false;
}
return true;
}
void klskr()
{
int sum = 0;
sort(ls,ls + num,cmp);
for(int i = 0; i < num; i++)
{
if(!lj(ls[i].u,ls[i].v))
sum += ls[i].w;
}
printf("%d\n",sum);
}
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
num = 0;
memset(lk,0,sizeof(lk));
memset(mp,'\0',sizeof(mp));
cnt = 1;
scanf("%d%d",&m,&n);
gets(mp[0]);
for(int i = 0; i < n; i++)
gets(mp[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(mp[i][j] == 'A' || mp[i][j] == 'S')
{
lk[i][j] = cnt;
rb[cnt].x = i;
rb[cnt].y = j;
rb[cnt].step = cnt;
cnt++;
}
}
}
for(int i = 1; i < cnt; i++)
{
bfs(rb[i].step,rb[i].x,rb[i].y,n,m);
}
for(int i = 0; i < cnt; i++)
d[i] = i;
klskr();
}
return 0;
}