Borg Maze
2016-07-14 18:46
369 查看
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12254 Accepted: 4023
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
8
11
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001
题意:
从S点开始,去找每个A,在每个A处可以分开去找其他的A.#表示墙,空格可以走。
分析:
如果知道了每个A和S之间的距离,这道题就是标准的求最小生成树的题目。
那如何去找他们之间的距离了,那就要使用广搜了。还需要注意的是如何为标识每个A,所以我们按照输入的顺序给每个A编号(从1开始)。剩下的就只需要用kruskal算法求最小生成树就可以了。
AC代码:
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12254 Accepted: 4023
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
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001
题意:
从S点开始,去找每个A,在每个A处可以分开去找其他的A.#表示墙,空格可以走。
分析:
如果知道了每个A和S之间的距离,这道题就是标准的求最小生成树的题目。
那如何去找他们之间的距离了,那就要使用广搜了。还需要注意的是如何为标识每个A,所以我们按照输入的顺序给每个A编号(从1开始)。剩下的就只需要用kruskal算法求最小生成树就可以了。
AC代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> using namespace std; const int maxn=60; const int maxp=110; struct Edge { int u,v,w; }; struct point { int x,y,step; }; Edge E[maxp*maxp]; int map[maxn][maxn]; int v[maxn][maxn]; int p[maxp]; int n,m; int dir[4][2]={0,-1,0,1,-1,0,1,0}; void bfs(int x,int y) { point now,next; now.x=x;now.y=y;now.step=0; queue<point> q; q.push(now); memset(v,0,sizeof(v)); v[x][y]=1; int num=1; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.step=now.step+1; if(!v[next.x][next.y] && map[next.x][next.y]>=0) { q.push(next); v[next.x][next.y]=1; if(map[next.x][next.y]>0) { E[m].u=map[x][y]; E[m].v=map[next.x][next.y]; E[m++].w=next.step; num++; if(num==n) return; } } } } return ; } int cmp(Edge a,Edge b) { return a.w<b.w; } int find_(int x) { return p[x]==x?x:p[x]=find_(p[x]); } int kruskal() { int ans=0; for(int i=1;i<=n;i++) p[i]=i; sort(E,E+m,cmp); int num=0; for(int i=0;i<m;i++) { int xx = find_(E[i].u); int yy = find_(E[i].v); if(xx!=yy) { p[xx]=yy; ans+=E[i].w; num++; } if(num>=n-1) break; } return ans; } int main() { int t,r,c; char temp[maxn]; char ch; scanf("%d",&t); while(t--) { scanf("%d%d",&c,&r); gets(temp); n=m=0; for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) { scanf("%c",&ch); if(ch=='#') map[i][j]=-1; else if(ch==' ') map[i][j]=0; else map[i][j]=++n; } getchar(); } for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) { if(map[i][j]>0) bfs(i,j); } } printf("%d\n",kruskal()); } return 0; }
相关文章推荐
- 当了一天首富就被“拉下马” ZARA创始人奥特加其人
- 轻松理解MYSQL MVCC 实现机制
- 面试题23:从上到下层次遍历二叉树
- Gradle1.5.0之后如何控制dex包内的方法数上限?
- surfaceview
- 加载多个dex
- fragment 懒加载
- GitHub for Windows使用教程(一)
- POJ3186 Treats for the Cows
- removeFromParent 和 addChild 和引用计数的关系
- 个人学习java的真实经验!
- 无功功率调节
- T和L的区别在哪里
- 敏捷开发
- android
- android调用系统相机并获取图片
- bzoj 2600: [Ioi2011]ricehub
- 07附加题 用两个队列实现栈
- android自定义组合控件步骤
- NSPredicate模糊查询