Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9821 | Accepted: 3283 |
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
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<algorithm> 5 #include <iostream> 6 using namespace std; 7 int map[102][102]; 8 int vis[102][102]; 9 int parent[102]; 10 int row, col; 11 int edge_num = 0; 12 int point_num=0; 13 struct edge { 14 int u, v, w; 15 } e[102 * 102]; 16 struct points { 17 int u, v, d; 18 } point; 19 bool cmp(const edge e1,const edge e2){ 20 return e1.w<e2.w; 21 } 22 int Kruskal(){ 23 int mindis=0; 24 for(int i=1;i<=point_num;i++){ 25 parent[i]=i; 26 } 27 sort(e,e+edge_num,cmp); 28 int pnum=0; 29 for(int i=0;i<edge_num&&pnum<point_num;i++){ 30 int k,g; 31 int u=e[i].u; 32 int v=e[i].v; 33 for(k=parent[u];parent[k]!=k;k=parent[parent[k]]); 34 for(g=parent[v];parent[g]!=g;g=parent[parent[g]]); 35 36 if(k!=g){ 37 parent[g]=k; 38 mindis+=e[i].w; 39 pnum++; 40 } 41 } 42 return mindis; 43 } 44 void BFS(int x, int y) { 45 queue<points> p; 46 points s; 47 s.u = x; 48 s.v = y; 49 s.d = 0; 50 p.push(s); 51 memset(vis, 0, 102 * 102 * sizeof(int)); 52 vis[x][y] = 1; 53 54 while (!p.empty()) { 55 points tmp = p.front(); 56 p.pop(); 57 for (int i = -1; i < 2; i += 2) { 58 points next; 59 next.u = tmp.u + i; 60 next.v = tmp.v; 61 next.d = tmp.d + 1; 62 if (next.u >= 0 && next.u < row) { 63 if (!vis[next.u][next.v]) { 64 int pos = map[next.u][next.v]; 65 vis[next.u][next.v] = 1; 66 if (pos >= 0) 67 p.push(next); 68 if (pos >= 1) { 69 e[edge_num].u = map[x][y]; 70 e[edge_num].v = pos; 71 e[edge_num].w = next.d; 72 edge_num++; 73 } 74 } 75 } 76 } 77 for (int i = -1; i < 2; i += 2) { 78 points next; 79 next.u = tmp.u; 80 next.v = tmp.v + i; 81 next.d = tmp.d + 1; 82 if (next.v >= 0 && next.v < col) { 83 if (!vis[next.u][next.v]) { 84 int pos = map[next.u][next.v]; 85 vis[next.u][next.v] = 1; 86 if (pos >= 0) 87 p.push(next); 88 if (pos >= 1) { 89 e[edge_num].u = map[x][y]; 90 e[edge_num].v = pos; 91 e[edge_num].w = next.d; 92 edge_num++; 93 } 94 } 95 } 96 } 97 98 } 99 } 100 int main() { 101 int n; 102 scanf("%d", &n); 103 char tmp[102]; 104 for (int i = 0; i < n; i++) { 105 point_num=0; 106 edge_num=0; 107 scanf("%d %d", &col, &row); 108 gets(tmp); //坑【一串空格】 109 for (int j = 0; j < row; j++) { 110 for (int k = 0; k < col; k++) { 111 char c; 112 scanf("%c", &c); 113 if (c == ' ') 114 map[j][k] = 0; 115 else if (c == '#') 116 map[j][k] = -1; 117 else if (c == ' ') 118 k--; 119 else 120 map[j][k] = ++point_num; 121 } 122 123 } 124 for (int j = 0; j < row; j++) { 125 for (int k = 0; k < col; k++) { 126 if (map[j][k] > 0) { 127 BFS(j, k); 128 } 129 } 130 } 131 int mid=Kruskal(); 132 printf("%d ",mid); 133 } 134 return 0; 135 }