POJ 3016 Borg Maze(BFS+最小生成树)
2016-02-21 20:42
253 查看
Borg Maze
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
题解:
大意,给出 一个m*n的迷宫,#代表墙, 现在求从S到达所有A的最短路径和,重复的路径不计算;
思路:将所有A和S 看做节点,则可以构成一个 包含N个节点的无向完全连通图, 权值为字母到其他字母的距离;用BFS求出字母到字母的距离,用Prim求解图的最小生成树即可;
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11490 | Accepted: 3773 |
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
题解:
大意,给出 一个m*n的迷宫,#代表墙, 现在求从S到达所有A的最短路径和,重复的路径不计算;
思路:将所有A和S 看做节点,则可以构成一个 包含N个节点的无向完全连通图, 权值为字母到其他字母的距离;用BFS求出字母到字母的距离,用Prim求解图的最小生成树即可;
AC代码:
/* time: author:tpbluesky answer: */ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <set> #include <list> #include <map> #include <stack> #include <queue> #include <cmath> #include <cstdlib> #include <sstream> #define inf 0x3f3f3f3f #define eps 1e-6 #define sqr(x) ((x)*(x)) using namespace std; typedef long long ll; const int maxn = 105; char mp[maxn][maxn]; int n, m, cnt; int tmp[maxn][maxn], dis[maxn][maxn], vis[maxn][maxn]; int dx[] = {1,-1,0,0}; int dy[] = {0,0,1,-1}; struct node{ int x, y, s; node(int x,int y,int s){ this->x = x; this->y = y; this->s = s; } node(){} }; void bfs(int x,int y){ memset(vis,0,sizeof(vis)); int start = tmp[x][y]; queue<node> q; vis[x][y] = 1; q.push(node(x,y,0)); while(!q.empty()){ node t = q.front(); q.pop(); if(tmp[t.x][t.y]){ dis[start][tmp[t.x][t.y]] = t.s; } for(int i = 0;i < 4;++i){ int tx = t.x+dx[i], ty = t.y+dy[i]; if(tx >= 0 && ty >= 0 && tx < n && ty < m && !vis[tx][ty] && mp[tx][ty] != '#'){ vis[tx][ty] = 1; q.push(node(tx,ty,t.s+1)); } } } } int p[maxn], d[maxn]; int prim(){ int res = 0; memset(p,0,sizeof(p)); for(int i = 1;i <= cnt;++i) d[i] = dis[1][i]; p[1] = 1; while(1){ int x = -1, minl = inf; for(int i = 1;i <= cnt;++i) if(!p[i] && d[i] < minl) minl = d[x=i]; if(x == -1) break; res += minl; p[x] = 1; for(int i = 1;i <= cnt;++i) d[i] = min(d[i],dis[x][i]); } return res; } int main() { int T; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); gets(mp[0]); cnt = 0; memset(tmp,0,sizeof(tmp)); memset(dis,0,sizeof(dis)); for(int i = 0;i < n;++i){ gets(mp[i]); for(int j = 0;j < m;++j) if(mp[i][j] == 'A' || mp[i][j] == 'S') tmp[i][j] = ++cnt; } for(int i = 0;i < n;++i) for(int j = 0;j < m;++j){ if(tmp[i][j]) bfs(i,j); } printf("%d\n",prim()); } return 0; }
相关文章推荐
- SenchaTouch-Viewport
- shell编程之文本与日志过滤
- 系统开机启动过程
- 放肆地使用UIBezierPath和CAShapeLayer画各种图形
- 管理指针成员(智能指针)
- Win7 和 MAC 系统通过VMware共享文件夹
- HSV颜色空间
- C++中搜索、截取字符串
- SQL Server ->> 深入探讨SQL Server 2016新特性之 --- Temporal Table(历史表)
- MAC下使用brew安装lrzsz
- Java虚拟机学习 - 垃圾收集器
- hdu1028——Ignatius and the Princess III
- JS大总结
- 素数判定
- C++自制Redis 数据库(十二) 初级存储引擎已经测试完毕
- WebView
- Activity的四种launchMode
- Android 5.0新特性RecycleView的基本使用
- Spring MVC3在controller和视图之间传递参数的方法
- x264(20090216-2245)main函数级的流程图