POJ3026——Borg Maze
2016-08-15 16:10
417 查看
Borg Maze
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点的最小距离。#表示墙 空格表示可以通行;
思路:
最小生成树,把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 , gra ; int qx[NN], qy[NN], dis , 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 ; 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; }
相关文章推荐
- Prim(普里姆)算法求最小生成树的思想及C语言实例讲解
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 最小生成树算法之Prim算法
- 使用C语言实现最小生成树求解的简单方法
- 最小生成树算法——Prim和Kruskal算法的实现
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- Data Structure - Week 15
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列