• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(334) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            色综合久久久久综合99| 亚洲午夜久久久久久噜噜噜| 久久无码人妻一区二区三区| 欧美黑人又粗又大久久久| 精品综合久久久久久888蜜芽| 久久精品黄AA片一区二区三区| 久久久九九有精品国产| 伊人久久精品影院| 国产精品久久久久无码av| 久久久受www免费人成| 欧美黑人又粗又大久久久| 欧美久久一级内射wwwwww.| 日韩AV无码久久一区二区| 久久国产精品视频| 久久人人爽人人爽人人爽| 日韩精品久久久久久| 久久免费的精品国产V∧| 亚洲一区精品伊人久久伊人 | 欧美一级久久久久久久大| 奇米影视7777久久精品人人爽| 久久亚洲国产午夜精品理论片| 伊人久久综合无码成人网| 性做久久久久久久久久久| 国产叼嘿久久精品久久| 国产精品欧美久久久天天影视| 日产久久强奸免费的看| 精品久久久久久无码人妻热| 精品久久久久久综合日本| 久久久久久无码Av成人影院| 精品综合久久久久久98| 亚洲欧洲久久av| 亚洲欧美国产精品专区久久| 久久久久亚洲?V成人无码| 国产精品无码久久四虎| 麻豆精品久久精品色综合| 狠狠干狠狠久久| 精品久久久久久国产91| 色综合久久久久| 久久99久久无码毛片一区二区| 精品久久一区二区三区| 色噜噜狠狠先锋影音久久|