您的位置:首页 > 其它

poj-3026 Borg Maze BFS+最小生成树

2015-07-31 14:03 330 查看
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

题意是找到每个S和A和其他S或A之间走到所需要的步数,然后建立最小生成树,来统计访问所有A或S最少需要走多少步。这是最小生成树和BFS结合的题,一开始我是用DFS搜索的点,由于图数据和题目中给的范围不一样,可能很大。所以DFS复杂度较高会超时,之后用了BFS直接AC。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f

using namespace std;

struct node
{
int x,y;
}point[500];
struct  abc
{
int x,y,step;
}now,next,que [500];

char map_c[310][310];
int map_n[310][310];
int map[310][310];
int vis_n[310][310];
int vis[310];
int dis[310];
int n,m,p,z;

void prim()
{
int i,j;
int pos=1,sum=0,min;
for (i=1;i<z;i++)
{
dis[i]=map[pos][i];
}
vis[pos]=1;
for (i=1;i<z-1;i++)
{
min=INF;
for (j=1;j<z;j++)
{
if(min>dis[j]&&!vis[j])
{
pos=j;
min=dis[pos];
}
}
vis[pos]=1;
sum += min;
for (j=1;j<z;j++)
{
if(dis[j]>map[pos][j]&&!vis[j])
{
dis[j]=map[pos][j];
}
}
}
printf("%d\n",sum);
}

void BFS(int x,int y)
{
int s=0,e=0,i;
vis_n[x][y]=1;
que[e].x=x;
que[i].step=0;
que[e++].y=y;
while (s<e)
{
now=que[s++];
for (i=1;i<=4;i++)
{
if(i==1)
{
next.x=now.x+1;
next.y=now.y;
}
if(i==2)
{
next.x=now.x-1;
next.y=now.y;
}
if(i==3)
{
next.x=now.x;
next.y=now.y+1;
}
if(i==4)
{
next.x=now.x;
next.y=now.y-1;
}
next.step = now.step+1;
if(next.x>=0&&next.y>=0&&next.x<m&&next.y<n&&!vis_n[next.x][next.y]&&map_n[next.x][next.y] >=0)
{
vis_n[next.x][next.y]=1;
if(map_n[next.x][next.y] >0&&map[p][map_n[next.x][next.y]]==0)
{
map[p][map_n[next.x][next.y]]=next.step;
map[map_n[next.x][next.y]][p]=next.step;
}
que[e++]=next;
}
}
}
}

 int main()
{
int t,i,j;
scanf("%d",&t);
while (t--)
{
z=1;
memset(vis,0,sizeof(vis));
memset(vis_n,0,sizeof(vis_n));
memset(map_n,0,sizeof(map_n));
memset(map,0,sizeof(map));
scanf("%d%d",&n,&m);
gets(map_c[0]);
for (i=0;i<m;i++)
{
gets(map_c[i]);
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if(map_c[i][j]=='A'||map_c[i][j]=='S')
{
point[z].x=i;
point[z].y=j;
map_n[i][j]=z++;
}
else if(map_c[i][j]=='#') map_n[i][j]=-1;
else if(map_c[i][j]==' ') map_n[i][j]=0;
}
}
for (i=1;i<z;i++)
{
memset(vis_n,0,sizeof(vis_n));
int x=point[i].x;
int y=point[i].y;
p=i;
BFS(x,y);
}
prim();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: