poj3026

Borg Maze

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4902 Accepted: 1659

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

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11
 
这是ACM算法训练里初级中最后一个MST的题目了
鸭梨好大……
题目果断没看懂啊
不过看他的样例,隐约猜到了做法
任意两个点之间的距离(floodfill),然后求最小生成树
然后去找翻译,果然跟我想的一样
任意两点的距离怎么求呢,对每一点floodfill 因为是bfs嘛,所以最先得到的距离就是某一点距离这一点的最短距离
这样做效率不高,因为任意两点之间的距离是一定的,所以任意两点的距离都被求了两边
没想到什么好的优化方法
1次AC 挺爽的
 
  1#include<stdio.h>
  2#include<string.h>
  3#include<math.h>
  4#define MAX 150
  5struct node
  6{
  7    int x,y,s;
  8}
;
  9int dx[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
 10int n,m,ans;
 11short map[55][55];
 12int num,dis[MAX][MAX];
 13void init()
 14{
 15    int i,j;
 16    char tmp[100];
 17    scanf("%d%d",&m,&n);
 18    gets(tmp);
 19    num=1;
 20    for (i=0; i<n ; i++ )
 21    {
 22        gets(tmp);
 23        for (j=0; j<m ; j++ )
 24        {
 25            if (tmp[j]=='#') map[i][j]=-1;
 26            else if (tmp[j]==' ') map[i][j]=0;
 27            else if (tmp[j]=='S') map[i][j]=1;
 28            else if (tmp[j]=='A')
 29            {
 30                num++;
 31                map[i][j]=num;
 32            }

 33        }

 34    }

 35}

 36void bfs(int x,int y)
 37{
 38    int head,tail,i,j;
 39    short flag[55][55];
 40    struct node q[3000],now,nn;
 41    head=0;
 42    tail=1;
 43    q[tail].x=x;
 44    q[tail].y=y;
 45    q[tail].s=0;
 46    memset(flag,0,sizeof(flag));
 47    flag[x][y]=1;
 48    while (head!=tail)
 49    {
 50        head++;
 51        now=q[head];
 52        for (i=0; i<4 ; i++ )
 53        {
 54            nn.x=now.x+dx[i][0];
 55            nn.y=now.y+dx[i][1];
 56            if ((nn.x>=0)&&(nn.x<n)&&(nn.y>=0)&&(nn.y<m)&&(!flag[nn.x][nn.y])&&(map[nn.x][nn.y]>=0))
 57            {
 58                nn.s=now.s+1;
 59                tail++;
 60                flag[nn.x][nn.y]=1;
 61                q[tail]=nn;
 62                if (map[nn.x][nn.y]>0)
 63                {
 64                    dis[map[x][y]][map[nn.x][nn.y]]=nn.s;
 65                    dis[map[nn.x][nn.y]][map[x][y]]=nn.s;
 66                }

 67            }

 68        }

 69    }

 70}

 71void prim()
 72{
 73    int cost[MAX];
 74    short vis[MAX];
 75    int min,mini,i,j;
 76    memset(vis,0,sizeof(vis));
 77    for (i=2; i<=num; i++) cost[i]=dis[1][i];
 78    vis[1]=1;
 79    ans=0;
 80    cost[1]=0;
 81    for (i=1; i<=num-1 ; i++ )
 82    {
 83        min=0x7fffffff;
 84        for (j=1; j<=num; j++ )
 85            if ((!vis[j])&&(cost[j]<min))
 86            {
 87                min=cost[j];
 88                mini=j;
 89            }

 90        ans=ans+min;
 91        vis[mini]=1;
 92        for (j=1; j<=num ; j++ )
 93            if ((!vis[j])&&(dis[mini][j]>0)&&(cost[j]>dis[mini][j]))
 94            {
 95                cost[j]=dis[mini][j];
 96            }

 97    }

 98}

 99void work()
100{
101    int i,j;
102    ans=0;
103    memset(dis,0,sizeof(dis));
104    for (i=0; i<n ; i++ )
105        for (j=0; j<n ; j++ )
106            if (map[i][j]>0)
107            {
108                bfs(i,j);
109            }

110    prim();
111    printf("%d\n",ans);
112}

113int main()
114{
115    int t;
116    scanf("%d",&t);
117    while (t)
118    {
119        init();
120        work();
121        t--;
122    }

123    return 0;
124}

125

posted on 2012-02-15 15:29 jh818012 阅读(325) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论