• <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嘛,所以最先得到的距離就是某一點距離這一點的最短距離
            這樣做效率不高,因為任意兩點之間的距離是一定的,所以任意兩點的距離都被求了兩邊
            沒想到什么好的優(yōu)化方法
            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 閱讀(335) 評論(0)  編輯 收藏 引用

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

            導航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(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久久亚洲综合精品| 亚洲日韩欧美一区久久久久我| 亚洲AV日韩精品久久久久久久| 国产精品免费久久| 日韩影院久久| 国内精品久久久久久99蜜桃| 狠狠色丁香久久婷婷综合蜜芽五月| 九九久久自然熟的香蕉图片| 99久久综合国产精品二区| 亚洲va久久久久| 性高朝久久久久久久久久| 色综合久久综合中文综合网| 久久久精品日本一区二区三区| 伊人久久大香线蕉影院95| 久久久久久免费一区二区三区| 韩国无遮挡三级久久| 久久成人国产精品二三区| 久久精品桃花综合| 久久久久女教师免费一区| 青青青青久久精品国产h| 久久婷婷国产综合精品| 97久久香蕉国产线看观看| 成人久久精品一区二区三区| 伊人久久大香线蕉成人| 久久综合视频网站| 日本久久久久亚洲中字幕| 久久久久久曰本AV免费免费| 无码人妻少妇久久中文字幕蜜桃| 久久夜色撩人精品国产| 777久久精品一区二区三区无码| 99精品久久精品| 国产精品久久一区二区三区| 久久国产精品77777| 久久93精品国产91久久综合 | 亚洲欧美精品一区久久中文字幕| 久久99精品综合国产首页| 狠狠色丁香婷婷综合久久来| 天天综合久久久网| 91精品婷婷国产综合久久| 久久天天躁狠狠躁夜夜不卡|