POJ3026——Borg Maze
来源:互联网 发布:广发淘宝卡集分宝 编辑:程序博客网 时间:2024/06/06 03:55
Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12904 Accepted: 4204
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.
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
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001
题目大意:
求从S出发 到各个A点的最小距离。#表示墙 空格表示可以通行;
思路:
最小生成树,把S看成和A等价的就好,首先 用广搜 把各个字母之间的距离存到图里,然后求该图的最小生成树就好了;
代码:
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>using namespace std;const int N = 110;const int NN = 2510;const int inf = 0x3f3f3f3f;int n,m;int mp[N][N], gra[N][N];int qx[NN], qy[NN], dis[N][N], vis[N/2][N/2];int dir[5][2] = {{0,-1},{0,1},{1,0},{-1,0}};void bfs(int x, int y){ memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); int head, tail, tx, ty; vis[x][y] = 1; dis[x][y] = 0; qx[0] = x; qy[0] = y; head = 0; tail = 1; while(head<tail) { tx = qx[head]; ty = qy[head]; head++; for( int i = 0;i < 4;i++ ) { int dx = tx + dir[i][0]; int dy = ty + dir[i][1]; if( dx>0&&dx< n&&dy>0&&dy<m&&!vis[dx][dy]&&mp[tx][ty]>=0) { dis[dx][dy] = dis[tx][ty] + 1; vis[dx][dy] = 1; qx[tail] = dx; qy[tail] = dy; tail++; if( mp[dx][dy]>0 ) { gra[mp[x][y]][mp[dx][dy]] = dis[dx][dy]; gra[mp[dx][dy]][mp[x][y]] = dis[dx][dy]; } } } }}int prim(int t,int num){ int vi[NN], d[NN]; memset(vi,0,sizeof(vi)); for( int i = 2;i <= num;i++ ) d[i] = inf; d[1] = 0; //vi[1] = 1; int sum = 0; for( int i = 1;i <= num;i++ ) { int mn = inf, x = -1; for( int j = 1;j <= num;j++ ) { if( !vi[j]&&d[j]<mn ) { mn = d[j]; x = j; } } if( x==-1 ) break; vi[x] = 1; sum += mn; for( int j = 1;j <= num;j++ ) { if( !vi[j]&&d[j]>gra[j][x] ) d[j] = gra[j][x]; } } return sum;}int main(){ int T; char s[N]; cin>>T; while( T-- ) { memset(gra,0,sizeof(gra)); cin>>n>>m; gets(s); int num = 1; for( int i = 0;i < m;i++ ) { gets(s); for( int j = 0;j < n;j++ ) { if( s[j]=='#' ) mp[i][j] = -1; else if( s[j]==' ' ) mp[i][j] = 0; else if( s[j]=='A' ) mp[i][j] = ++num; else if(s[j]=='S' ) mp[i][j] = 1; } } for( int i = 0;i < m;i++ ) { for( int j = 0;j < n;j++ ) { if( mp[i][j]>0 ) bfs(i, j); } } cout<<prim(1,num)<<endl; } return 0;}
0 0
- POJ3026——Borg Maze
- POJ3026——Borg Maze
- poj3026——Borg Maze(BFS+最小生成树)
- poj3026 Borg Maze
- poj3026 Borg Maze
- poj3026 Borg Maze
- poj3026 - Borg Maze
- Borg Maze poj3026
- Borg Maze(POJ3026)
- poj3026 Borg Maze
- poj3026--Borg Maze
- POJ3026 - Borg Maze
- poj3026 Borg Maze
- POJ3026 - Borg Maze [MST]
- poj3026 Borg Maze
- poj3026 Borg Maze
- POJ3026 Borg Maze
- POJ3026 Borg Maze
- html表单控件select的sel.length属性
- 大牛指点绝招
- (HDU 5792)2016 Multi-University Training Contest 5 World is Exploding (逆序数、顺序数、树状数组)
- javaScript实现图片动态轮流展示
- Android的一个BindView工具的实现
- POJ3026——Borg Maze
- 数据结构实验之链表四:有序链表的归并
- Unity学习笔记 2nd —— 摄像机那些事
- Jenkins系统设置
- 瓶颈ing
- UI标签在火狐浏览器中某些问题解决
- zblog主题 www.gridsmedia.com
- 弹出窗口的两种实现方式 PopupWindow 和 Activity
- Zblog开源项目菜鸟集成之路(一)