bfs+prim
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int x,y;
int map[102][102];
int wz[51][51],D[102][102];
char sg[51][51];
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int num;
queue <int> qx,qy;
void bfs(int i,int j)
{
int vi[102][102];
int x2,y2,x1,y1,k;
memset(vi,0,sizeof(vi));
memset(D,0,sizeof(D));
vi[i][j]=1;
qx.push(i);
qy.push(j);
while(!qx.empty())
{
x2=qx.front(); qx.pop();
y2=qy.front(); qy.pop();
if(wz[x2][y2])
map[wz[i][j]][wz[x2][y2]]=D[x2][y2];
for(k=0;k<=3;k++)
{
x1=x2+dir[k][0];
y1=y2+dir[k][1];
if(x1>=1&&x1<=y&&y1>=1&&y1<=x&&sg[x1][y1]!='#'&&!vi[x1][y1])
{
qx.push(x1);
qy.push(y1);
vi[x1][y1]=1;
D[x1][y1]=D[x2][y2]+1;
}
}
}
}
int prim()
{
int dist[102],vist[102];
int i,j,pos,min,sum;
memset(dist,0,sizeof(dist));
memset(vist,0,sizeof(vist));
for(i=1;i<=num;i++)
dist[i]=map[1][i];
vist[1]=1;
sum=0;
for(i=2;i<=num;i++)
{
min=9999999;
for(j=1;j<=num;j++)
if(!vist[j]&&min>dist[j])
{
min=dist[j];
pos=j;
}
sum+=min;
vist[pos]=1;
for(j=1;j<=num;j++)
if(!vist[j]&&dist[j]>map[pos][j])
dist[j]=map[pos][j];
}
return sum;
}
int main()
{
int t,i,j;
char str[51];
scanf("%d",&t);
while(t--)
{
memset(wz,0,sizeof(wz));
scanf("%d%d",&x,&y);
num=0;
gets(str);
for(i=1;i<=y;i++)
{
for(j=1;j<=x;j++)
{
scanf("%c",&sg[i][j]);
if(sg[i][j]=='A'||sg[i][j]=='S')
{
num++;
wz[i][j]=num;
}
}
getchar();
}
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i!=j)
map[i][j]=9999999;
else
map[i][j]=0;
for(i=1;i<=y;i++)
for(j=1;j<=x;j++)
if(wz[i][j])
bfs(i,j);
cout<<prim();
if(t)
cout<<endl;
}
return 0;
}
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6082 Accepted: 2038
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.
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