您的位置:首页 > 其它

POJ 3026 Borg Maze 最小生成树+BFS

2017-09-01 15:07 323 查看
           先用BFS求出点与点之间的距离,然后用kruskal算法求出答案

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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn=105;
int T,n,m,X,Y;
int p[105];
bool vis[maxn][maxn];
char maze[maxn][maxn];
int num[maxn][maxn];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct node
{
int x,y,step;
}now,nex;
bool ok(int x,int y)
{
if(maze[x][y]!='#'&&!vis[x][y]&&x>=0&&x<X&&y>=0&&y<Y)
return true;
return false;
}
struct edge
{
int u,v,w;
}e[10000];
void bfs(int x,int y)
{
now.x=x;
now.y=y;
now.step=0;
memset(vis,false,sizeof(vis));
vis[x][y]=true;
queue<node> que;
while(!que.empty())
que.pop();
que.push(now);
while(!que.empty())
{
now=que.front();
if(num[now.x][now.y]&&now.step!=0)
{
e[++m].u=num[x][y];
e[m].v=num[now.x][now.y];
e[m].w=now.step;
//printf("%d %d %d %d %d\n",x,y,now.x,now.y,now.step);
}
que.pop();
for(int i=0;i<4;i++)
{
int nx=now.x+dx[i];
int ny=now.y+dy[i];
if(ok(nx,ny))
{
nex.x=nx;
nex.y=ny;
nex.step=now.step+1;
vis[nx][ny]=true;
que.push(nex);
}
}
}
return ;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int kruskal()
{
int ans=0;
sort(e+1,e+1+m,cmp);
// for(int i=1;i<=m;i++)
// printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
int cnt=0;
for(int i=1;i<=n;i++)
p[i]=i;
for(int i=1;i<=m;i++)
{
int t1=find(e[i].u);
int t2=find(e[i].v);
if(t1!=t2)
{
p[t1]=t2;
ans+=e[i].w;
cnt++;;
if(cnt==n-1)
break;
}
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&Y,&X);
n=m=0;
memset(num,0,sizeof(num));
gets(maze[0]);
for(int i=0;i<X;i++)
{
gets(maze[i]);
for(int j=0;j<Y;j++)
{
if(maze[i][j]=='A'||maze[i][j]=='S')
{
num[i][j]=++n;
// printf("%d\n",num[i][j]);
}
}
}
for(int i=0;i<X;i++)
{
for(int j=0;j<Y;j++)
{
if(num[i][j])
{
bfs(i,j);
}
}
}
// printf("%d %d\n",n,m);
printf("%d\n",kruskal());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: