poj3026--Borg Maze

来源:互联网 发布:python php 开发效率 编辑:程序博客网 时间:2024/04/28 01:21
Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7893 Accepted: 2637

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.

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
在迷宫中,连接所有的字母,对字母编号,用bfs求出所有字母对除自身的距离,得到所有字母的矩阵,用prim得出最短路,
在连接中是允许重复路径的,注意,一个坑,再输入x,y后还有空格,直接用getchar();错误
#include <stdio.h>#include <string.h>#define maxint 9999999char str[60][60] ;int p[60][60] ;int m[4000][4000] ;int dis[4000] , vis[4000] ;struct node{    int i , j , temp ;} q[4000];int x , y , n ;int s[][2] = { {1,0},{-1,0},{0,1},{0,-1} } ;void f(int i,int j){    int k = p[i][j] ;    int flag[60][60] ;    memset(flag,0,sizeof(flag));    m[k][k] = 0 ;    int low = 0 , top = 1 ;    q[0].i = i ;    q[0].j = j ;    q[0].temp = 0 ;    flag[i][j] = 1 ;    while(low < top)    {        node l = q[low++] ;        if( p[l.i][l.j] > 0 )            m[k][ p[l.i][l.j] ] = l.temp ;        for(i = 0 ; i < 4 ; i++)        {            int u , v ;            u = l.i + s[i][0] ;            v = l.j + s[i][1] ;            if(u >= 0 && u < x && v >= 0 && v < y && !flag[u][v] && p[u][v] >= 0)            {                flag[u][v] = 1 ;                q[top].i = u ;                q[top].j = v ;                q[top++].temp = l.temp+1 ;            }        }    }}int F(){    int i , j , ans = 0 ;    for(i = 1 ; i <= n ; i++)    {        dis[i] = m[1][i] ;        vis[i] = 0 ;    }    vis[1] = 0 ;    for(i = 1 ; i <= n ; i++)    {        int k = 0 , min_ans = maxint ;        for(j = 1 ; j <= n ; j++)        {            if( !vis[j] && dis[j] < min_ans )            {                k = j ;                min_ans = dis[j] ;            }        }        if(min_ans == maxint)            break;        vis[k] = 1 ;        ans += min_ans ;        for(j = 1 ; j <= n ; j++)            if( !vis[j] && dis[j] > m[k][j] )                dis[j] = m[k][j] ;    }    return ans ;}int main(){    int t , i , j , ans ;    char ss[10000] ;    scanf("%d", &t);    while(t--)    {        scanf("%d %d", &y, &x);        gets(ss);        for(i = 0 ; i < x ; i++)            gets(str[i]);        n = 0 ;        for(i = 0 ; i < x ; i++)            for(j = 0 ; j < y ; j++)                if(str[i][j] == '#')                    p[i][j] = -1 ;                else if(str[i][j] == ' ')                    p[i][j] = 0 ;                else                {                    n++ ;                    p[i][j] = n ;                }        for(i = 1 ; i <= n ; i++)            for(j = 1 ; j <= n ; j++)                m[i][j] = maxint ;        for(i = 0 ; i < x ; i++)            for(j = 0 ; j < y ; j++)                if(p[i][j] > 0)                    f(i,j);        ans = F();        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击