Poj 3026 -- Borg maze

Source: Internet
Author: User
Borg maze
Time limit:1000 ms   Memory limit:65536 K
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 conducting 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

There is a strong biological individual Borg in the Milky Way, and each individual has a connection. Let's help write a program to scan the entire Maze and assimilation the minimum cost of different individuals hidden in the maze.
A Represents different individuals. Space represents nothing, # represents an obstacle, and s is the start point. The scan can be up, down, or left.
Therefore, we need to first BFs to find the distance from each different individual to other different individuals, and then use MST to find the minimum cost.
Note:

1. There may be extra spaces after two numbers, so ignore these spaces. Otherwise, try again.
2. The branches must be removed during search. Otherwise, timeout occurs.
3. Thanks to the space help of discuss. Otherwise, this question will take 10 thousand years.

  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 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.