poj 3026 -- Borg Maze
Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7994 | Accepted: 2674 |
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
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Sample Output
8 11
题目大意:在银河系中有一强大生物个体Borg,每个个体之间都有一种联系。让我们帮忙写个程序扫描整个迷宫并同化隐藏在迷宫的相异个体的最小代价。
A 代表相异个体。空格代表什么没有,#代表障碍,S为开始点。扫描可以上下左右。
所以这个题就是要先BFS求出每个相异个体到其他相异个体的距离,然后用MST求出最小代价即可。
需要注意的地方:
1. 两个数字之后可能有多余的空格,所以要忽略掉这些空格,否则你提交试试看。
2.搜索时要减枝。否则会超时。
3.感谢discuss的空格帮助,否则这题会wrong一万年。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : BorgMaze.cpp 4 * Creat time : 2014-07-10 08:04 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 100 15 #define INF 0x7f7f7f7f 16 using namespace std; 17 int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; 18 char str[M][M]; 19 int n,xi,yj,cnt,dis[M*M]; 20 struct Node 21 { 22 int x,y; 23 }node[M*5]; 24 int c[M][M]; 25 void BFS(int x) 26 { 27 queue<Node> que; 28 int cnt1[M][M],vis[M][M],num = 0; 29 que.push(node[x]); 30 clr(vis,0); 31 clr(cnt1,0); 32 vis[node[x].x][node[x].y] = 1; 33 while(!que.empty()){ 34 Node B; 35 Node A = que.front(); 36 que.pop(); 37 if(str[A.x][A.y] == 'A'){ 38 for(int i = 0; i < cnt; i++){ 39 if(x < i && A.x == node[i].x && A.y == node[i].y){ 40 c[i][x] = c[x][i] = cnt1[A.x][A.y]; 41 num++; 42 } 43 } 44 } 45 if(num == cnt-1-x) break; 46 for(int i = 0; i < 4; i++){ 47 int xx = A.x + dir[i][0]; 48 int yy = A.y + dir[i][1]; 49 if(xx >= 0 && xx < yj && yy >=0 && yy < xi && (str[xx][yy] == ' ' || str[xx][yy] == 'A' || str[xx][yy] == 'S') && !vis[xx][yy]){ 50 B.x = xx; B.y = yy; 51 que.push(B); 52 cnt1[xx][yy] = cnt1[A.x][A.y] + 1; 53 vis[xx][yy] = 1; 54 } 55 } 56 } 57 } 58 void BuildGrap() 59 { 60 for(int i = 0; i < cnt; i++){ 61 BFS(i); 62 } 63 } 64 int prim() 65 { 66 int i,j,k,sum = 0; 67 bool vis[cnt+10]; 68 for(i = 0; i < cnt; i++){ 69 dis[i] = c[0][i]; 70 vis[i] = false; 71 } 72 vis[0] = true; 73 for(i = 1; i < cnt; i++){ 74 int _min = INF; 75 j = 0; 76 for(k = 0; k < cnt; k++){ 77 if(!vis[k] && _min > dis[k]){ 78 _min = dis[k]; 79 j = k; 80 } 81 } 82 vis[j] = true; 83 sum += dis[j]; 84 for(k = 0; k <cnt; k++){ 85 if(!vis[k] && dis[k] > c[j][k]){ 86 dis[k] = c[j][k]; 87 } 88 } 89 } 90 return sum; 91 } 92 int main(int argc,char *argv[]) 93 { 94 scanf("%d",&n); 95 while(n--){ 96 clr(str,0); 97 scanf("%d%d",&xi,&yj); 98 while(getchar()!='\n') 99 ; 100 cnt = 1; 101 for(int i = 0; i < yj; i++){ 102 for(int j = 0; j < xi; j++){ 103 scanf("%c",&str[i][j]); 104 if(str[i][j] == 'A'){ 105 node[cnt].x = i; node[cnt++].y = j; 106 } 107 if(str[i][j] == 'S'){ 108 node[0].x = i; node[0].y = j; 109 } 110 } 111 getchar(); 112 } 113 BuildGrap(); 114 int ans = prim(); 115 printf("%d\n",ans); 116 } 117 return 0; 118 }
Do one thing , and do it well !