您的位置:首页 > 其它

POJ 3026 bfs+最小生成树的应用

2016-02-03 09:45 260 查看
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
#include <IOSTREAM>
#include <QUEUE>
#include <CSTDLIB>
using namespace std;

#define MAXX 800
#define INF 9999999

int edge[MAXX][MAXX];
int nodemap[MAXX][MAXX];
char mapp[MAXX][MAXX];
int dist[MAXX][MAXX];
bool visit[MAXX][MAXX];
int row,col;
int path[4][2]={{-1,0},{1,0},{0,1},{0,-1}};

struct node
{
int row;
int col;
};

void build_graph(int xb,int yb) //用bfs建图,生成几个点的距离放在edge[][]中
{
memset(dist,0,sizeof(dist));
memset(visit,false,sizeof(visit));
node start_node;
start_node.row=xb;start_node.col=yb;
queue<node> qnode;
visit[xb][yb]=true;
qnode.push(start_node);
while (!qnode.empty())
{
node x=qnode.front();
if(nodemap[x.row][x.col])
edge[nodemap[xb][yb]][nodemap[x.row][x.col]]=dist[x.row][x.col];
qnode.pop();
for (int ic=0;ic<4;++ic)
{
int del_x=x.row+path[ic][0];
int del_y=x.col+path[ic][1];
if(del_x>=0&&del_x<row&&del_y>=0&&del_y<col)
if (!visit[del_x][del_y]&&mapp[del_x][del_y]!='#')
{
node new_node;new_node.row=del_x;new_node.col=del_y;
visit[del_x][del_y]=true;
dist[del_x][del_y]=dist[x.row][x.col]+1;
qnode.push(new_node);
}
}
}
}

int prim(int startx,int num) //最小生成树 prim算法
{
bool has_visit[MAXX];
int ic;
memset(has_visit,false,sizeof(has_visit));
int lowest,next=startx,found=1,maximum=0,temp;
has_visit[startx]=true;
int distance[MAXX];
for (ic=0;ic<=num;ic++)
distance[ic]=INF;
while (found!=num)
{
lowest=INF;
for (ic=2;ic<=num;ic++)
{
if (!has_visit[ic]&&edge[next][ic]<distance[ic])
distance[ic]=edge[next][ic];
if (!has_visit[ic]&&distance[ic]<lowest)
{
lowest=distance[ic];
temp=ic;
}
}
has_visit[temp]=true;
maximum+=lowest;
next=temp;
found++;
}
return maximum;
}

int main()
{
int cases=0,N,num=0;
int ic,j;
cin>>N;
while (cases!=N)
{
memset(mapp,0,sizeof(mapp));
memset(edge,0,sizeof(edge));
memset(nodemap,0,sizeof(nodemap));
num=0;
cin>>col>>row;
char temp[50];
gets(temp);
for (ic=0;ic<row;ic++)
gets(mapp[ic]);
for (ic=0;ic<row;ic++)
for (j=0;j<col;j++)
if (mapp[ic][j]=='S'||mapp[ic][j]=='A')
nodemap[ic][j]=++num;
for (ic=0;ic<row;ic++)
for (j=0;j<col;j++)
if (mapp[ic][j]=='S'||mapp[ic][j]=='A')
build_graph(ic,j);
cout<<prim(1,num)<<endl;
cases++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: