Borg Maze
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
题目大意:
大概思路:用 bfs 求出每个点之间的距离,再用prim去找最小生成树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e3+5,inf=0x3f3f3f3f;
int map[N][N],vis[N][N],dist[N],p[N][N],flag[N];
//map用来记录字母A或S之间的权值,p[i][j] i,j 是字母A或S的位置坐标,p是记录该字母是第几个字母
char mp[N][N];//用来记录每个位置的字符;
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};
int sum,n,m,ans;
struct node
{
int x,y,step;
};
void bfs(int a,int b)
{
memset(vis,0,sizeof(vis));
node f,t;
f.x=a,f.y=b,f.step=0;
vis[a][b]=1;
queue<node>q;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(mp[f.x][f.y]=='A'||mp[f.x][f.y]=='S')
map[p[a][b]][p[f.x][f.y]]=f.step;//记录第p[a][b]个字母到第p[f.x][f.y]个字母的距离
for(int i=0; i<4; i++)
{
t.x=f.x+dx[i],t.y=f.y+dy[i];
if(mp[t.x][t.y]!='#'&&vis[t.x][t.y]==0&&t.x<m&&t.x>=1&&t.y<n&&t.y>=1)
{
vis[t.x][t.y]=1,t.step=f.step+1;
q.push(t);
}
}
}
}
void prepare()
{
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
if(i==j)
map[i][j]=0;
else
map[i][j]=inf;
}
void prim()
{
sum=0;
for(int i=1; i<=ans; i++)
dist[i]=map[1][i],flag[i]=0;
flag[1]=1;
for(int i=2; i<=ans; i++)
{
int minn=inf,k;
for(int j=1; j<=ans; j++)
if(!flag[j]&&dist[j]<minn)
minn=dist[j],k=j;
//if(minn==inf)
// break;
sum+=minn;
flag[k]=1;
for(int j=1; j<=ans; j++)
if(!flag[j]&&dist[j]>map[k][j])
dist[j]=map[k][j];
}
cout<<sum<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ans=0;
char str[17];//str[]最短是15,别问我是怎么知道的 问就是一把心酸一把泪 嘤嘤嘤
cin>>n>>m;//m后面有很多空格 我的天
gets(str);//吃掉空格
memset(map,0,sizeof(map));
memset(mp,0,sizeof(mp));
int num=0;
for(int i=1; i<=m; i++)
{
getchar();
for(int j=1; j<=n; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='A'||mp[i][j]=='S')
{
num++,ans++;
p[i][j]=num;
}
}
}
prepare();
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
if(mp[i][j]=='A'||mp[i][j]=='S')
bfs(i,j);
prim();
}
return 0;
}