poj-3026-Borg Maze-最小生成树

来源:互联网 发布:windows to go怎么用 编辑:程序博客网 时间:2024/05/17 03:55

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


re了十几遍,不知道怎么回事,把数组放到1500,就过了,看了别人的都500多,就过,不明白,不明白啊啊啊啊啊啊

#include<stdio.h>#include<string.h>#include<stdlib.h>#define MAX 0x3f3f3fint n, m;int cont;int dx[4]= {1,-1,0,0};int dy[4] = {0,0,1,-1};char ls[350][350];int map[350][350];int mp[350][350];int bj[350][350];int vis[350];struct node{    int x,y;    int step;} ll[1500];void bfs(int r, int t){    memset(bj,0,sizeof(bj));    int tp = 0, tl = 1;    int i;    ll[0].x = r;    ll[0].y = t;    ll[0].step = 0;    bj[r][t] = 1;    while(tp < tl)    {        int x = ll[tp].x;        int y = ll[tp].y;        int step = ll[tp++].step;           if(map[x][y] && mp[map[r][t]][map[x][y]]==0)            {                mp[map[r][t]][map[x][y]] = step;                mp[map[x][y]][map[r][t]] = step;            }        for(i = 0; i < 4; i++)        {            int tx = x+dx[i];            int ty = y+dy[i];            if(tx >= 0 && tx < n && ty >= 0 && ty < m && ls[tx][ty] != '#' && !bj[tx][ty])            {                ll[tl].x = tx;                ll[tl].y = ty;                ll[tl++].step = step+1;                bj[tx][ty] = 1;            }        }    }}void prim(){    memset(vis,0,sizeof(vis));    int dis[350];    int i ;    int pos = 1;    for(i = 1; i <= cont ; i++)        dis[i] = mp[pos][i];    vis[pos] = 1;    int ans = 0;    for(i = 1; i < cont ; i++)    {        int min = MAX;        for(int j = 1; j <= cont ; j++)        {            if(min > dis[j] && !vis[j])            {                min = dis[ pos = j];            }        }        ans+=min;        vis[pos] = 1;        for(int j = 1; j <= cont ; j++)        {            if(dis[j] > mp[pos][j] && !vis[j])            {                dis[j] = mp[pos][j];            }        }    }    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        char mr[2];        memset(map,0,sizeof(map));        memset(mp,0,sizeof(mp));        scanf("%d%d",&m,&n);        int i, j;        gets(mr);        cont = 0;        for(i = 0; i < n; i++)        {            gets(ls[i]);            for(j = 0; j < m; j++)                if(ls[i][j] == 'A' || ls[i][j] == 'S')                    map[i][j] = ++cont;        }        /* for(i = 1; i <= cont; i++)             for(j = 1; j <= cont ; j++)                 if(i != j)                     mp[i][j] = MAX;*/        for(i = 0; i < n; i++)        {            for(j = 0; j < m; j++)            {                if(map[i][j])                    bfs(i,j);            }        }        prim();    }    return 0;}


0 0
原创粉丝点击