poj-3026 Borg Maze
2015-08-18 15:23
316 查看
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
题意:计算从S 到每个点A的最小总距离 <重复的路径不计算>
思路:<来源于 Jun博客>
将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;则 问题可看做 求 改图的最小生成树;利用Prim求解即可;
代码::/****
我认为这道题难想也是难理解的就是建距离图那一块 在这个代码中 自己设置点 即mp[][]=dot 在计算距离的时候 是计算点i到dot的距离
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 99999999
char str[300][300];
int mp[300][300],dot,dx[]= {-1,0,0,1},dy[]= {0,-1,1,0},n,m;///cnt用于BFS中的结束队列
int dis[300][300],vist[300][300];
struct node
{
int x,y,step;
} s[500];
int panduan(int a,int b)
{
if( a<1 || a>n || b<1 || b>m || vist[a][b]==1 || mp[a][b]==-1 )
return 0;
return 1;
}
int bfs()///BFS查找与建立距离图 <注意看建距离图怎么建的,>
{
node next,temp;
int x,y,i,j;
queue<node>q;
memset(dis,0,sizeof(dis));///计算结构体里面字符之间的距离
for(i=1; i<=dot; i++)///计算每个点到其他点的距离
{
while(!q.empty())
q.pop();
memset(vist,0,sizeof(vist));///bfs里面的标记数组
s[i].step = 0;
vist[s[i].x][s[i].y]=1;///表示以s[i].x为横坐标,s[i].y为纵坐标的点已经被搜索到
q.push(s[i]);
while(!q.empty())
{
temp=q.front();
q.pop();
x=temp.x;
y=temp.y;
if(mp[x][y]!=0 && mp[x][y]!=-1)///表示这个点数是A 或者 S
{
// cnt++;
dis[i][mp[x][y]]=dis[mp[x][y]][i]=temp.step;
/*if( cnt==dot)
break;*/
}
for(j=0; j<4; j++)///四个方向
{
int xx=temp.x+dx[j];
int yy=temp.y+dy[j];
if(panduan(xx,yy))
{
next.x=xx;
next.y=yy;
next.step=temp.step+1;
vist[xx][yy]=1;
q.push(next);
}
}
}
}
}
void prim()
{
int low[300],k,i,j,min1;
int vis[300],sum;
memset(vis,0,sizeof(vis));
for(i=1; i<=dot; i++)
{
low[i]= INF;///注意初始化
}
sum=0;
low[1]=0;///注意初始化
// vis[1]=1;
for(i=1; i<=dot; i++)
{
min1=INF;
for(j=1; j<=dot; j++)
{
if(low[j]<min1 && vis[j]==0)
{
min1=low[j];
k=j;
}
}
sum+=min1;
vis[k]=1;
for(j=1; j<=dot; j++)
{
if(vis[j]==0&&low[j]>dis[k][j])
{
low[j]=dis[k][j];
}
}
}
printf("%d\n",sum);
}
int main()
{
int t,i,j;
char c[60];
scanf("%d",&t);
while(t--)
{
dot=0;
scanf("%d%d",&n,&m);
gets(c);
for(i=1; i<=n; i++)
{
gets(str[i]);
for(j=1; j<=m; j++)
{
if(str[i][j]=='A'||str[i][j]=='S')///把含有A S字符的存进结构体
{
dot++;
mp[i][j]=dot; ///方便bfs里面建距离图 mp[i][j]为距离图的横坐标
s[dot].x=i;
s[dot].y=j;
}
else if(str[i][j]==' ')
{
mp[i][j]=0;
}
else if(str[i][j]=='#')
{
mp[i][j]=-1;
}
}
}
bfs();
prim();
}
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
题意:计算从S 到每个点A的最小总距离 <重复的路径不计算>
思路:<来源于 Jun博客>
将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;则 问题可看做 求 改图的最小生成树;利用Prim求解即可;
代码::/****
我认为这道题难想也是难理解的就是建距离图那一块 在这个代码中 自己设置点 即mp[][]=dot 在计算距离的时候 是计算点i到dot的距离
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 99999999
char str[300][300];
int mp[300][300],dot,dx[]= {-1,0,0,1},dy[]= {0,-1,1,0},n,m;///cnt用于BFS中的结束队列
int dis[300][300],vist[300][300];
struct node
{
int x,y,step;
} s[500];
int panduan(int a,int b)
{
if( a<1 || a>n || b<1 || b>m || vist[a][b]==1 || mp[a][b]==-1 )
return 0;
return 1;
}
int bfs()///BFS查找与建立距离图 <注意看建距离图怎么建的,>
{
node next,temp;
int x,y,i,j;
queue<node>q;
memset(dis,0,sizeof(dis));///计算结构体里面字符之间的距离
for(i=1; i<=dot; i++)///计算每个点到其他点的距离
{
while(!q.empty())
q.pop();
memset(vist,0,sizeof(vist));///bfs里面的标记数组
s[i].step = 0;
vist[s[i].x][s[i].y]=1;///表示以s[i].x为横坐标,s[i].y为纵坐标的点已经被搜索到
q.push(s[i]);
while(!q.empty())
{
temp=q.front();
q.pop();
x=temp.x;
y=temp.y;
if(mp[x][y]!=0 && mp[x][y]!=-1)///表示这个点数是A 或者 S
{
// cnt++;
dis[i][mp[x][y]]=dis[mp[x][y]][i]=temp.step;
/*if( cnt==dot)
break;*/
}
for(j=0; j<4; j++)///四个方向
{
int xx=temp.x+dx[j];
int yy=temp.y+dy[j];
if(panduan(xx,yy))
{
next.x=xx;
next.y=yy;
next.step=temp.step+1;
vist[xx][yy]=1;
q.push(next);
}
}
}
}
}
void prim()
{
int low[300],k,i,j,min1;
int vis[300],sum;
memset(vis,0,sizeof(vis));
for(i=1; i<=dot; i++)
{
low[i]= INF;///注意初始化
}
sum=0;
low[1]=0;///注意初始化
// vis[1]=1;
for(i=1; i<=dot; i++)
{
min1=INF;
for(j=1; j<=dot; j++)
{
if(low[j]<min1 && vis[j]==0)
{
min1=low[j];
k=j;
}
}
sum+=min1;
vis[k]=1;
for(j=1; j<=dot; j++)
{
if(vis[j]==0&&low[j]>dis[k][j])
{
low[j]=dis[k][j];
}
}
}
printf("%d\n",sum);
}
int main()
{
int t,i,j;
char c[60];
scanf("%d",&t);
while(t--)
{
dot=0;
scanf("%d%d",&n,&m);
gets(c);
for(i=1; i<=n; i++)
{
gets(str[i]);
for(j=1; j<=m; j++)
{
if(str[i][j]=='A'||str[i][j]=='S')///把含有A S字符的存进结构体
{
dot++;
mp[i][j]=dot; ///方便bfs里面建距离图 mp[i][j]为距离图的横坐标
s[dot].x=i;
s[dot].y=j;
}
else if(str[i][j]==' ')
{
mp[i][j]=0;
}
else if(str[i][j]=='#')
{
mp[i][j]=-1;
}
}
}
bfs();
prim();
}
return 0;
}
相关文章推荐
- 【Mockplus教程】MAC 安全提示无法安装怎么办?
- POJ 2186-Popular Cows
- 纹理滤波、灰度共生矩阵
- storm集群的安装和部署
- 端口镜像和链路汇聚
- c#基础之数组
- 如何查看存储过程中动态生成的sql
- 解决用VideoView 播放一些视频无法播放问题
- 回文数猜想
- freeSwitch1.7+vBilling centos6.6 mini install
- window.onresize(当你改变窗口大小的时候会触发这个事件)
- SurfaceView和View区别总结
- 比较简单和理解的下拉框联动实现
- git的使用
- LINQ To SQL 语法及实例大全【转】
- [Java] public, private, protected
- PhantomJS linux系统下安装步骤及使用方法(网页截屏功能)
- c#基础之数组
- oc 中的.m和.mm文件区别
- android 一个公共的适配器,简化适配器代码