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
Sample Output
#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;
}
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
811
#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;
}
相关文章推荐
- openstack neutron gre网络拓扑实验总结
- (14)shell 函数以及函数参数
- 安卓手机屏幕录像之scr
- Robot Framework与Web界面自动化测试学习笔记:利用xpath定位元素
- ExtJS 上传下载控件
- 十六进制 补码 源码
- InputStream与String/byte[]相互转换
- STS中取消show in Breadcrumb方法
- 4320: ShangHai2006 Homework 并查集+离线处理 思路题
- ES6学习——元数据(meta)编程:反射(Reflect)API介绍
- 在MyEclipse部署项目到tomcat中时,WEB-INF下的class文件夹是空的解决办法
- HDU 3265 Posters(线段树)
- 《1024伐木累》-放鞭炮,发财还是健康
- 秒杀系统架构分析与实战(深度学习资料)
- [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(四)
- C++小练习题,很有趣!很简单!-- 001 (咋个办呢 zgbn)
- 七层登录——小问题
- Oracle学习之常用对象(一)—表&视图&序列
- php eval函数一句话木马代码
- 推荐Pyramid