• <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
            • 評論內容較長,點擊標題查看
            • --王私江
            欧美日韩精品久久久免费观看| 色诱久久av| 久久精品成人免费网站| 91精品国产色综久久| 久久久久亚洲精品男人的天堂| 思思久久99热只有频精品66| 蜜臀av性久久久久蜜臀aⅴ| 久久久久四虎国产精品| 日韩欧美亚洲综合久久影院Ds| 性欧美大战久久久久久久久| 国产精品无码久久久久 | 麻豆AV一区二区三区久久| 91精品国产高清91久久久久久| 久久精品二区| 久久国产高清字幕中文| 久久精品桃花综合| 久久精品亚洲精品国产欧美| 久久大香香蕉国产| 亚洲伊人久久大香线蕉综合图片| 久久AAAA片一区二区| 国产精品久久久久aaaa| 久久无码AV一区二区三区| 精品99久久aaa一级毛片| 亚洲中文字幕无码久久2020| 久久人人爽人爽人人爽av| 国内精品久久人妻互换| 亚洲级αV无码毛片久久精品 | 狠狠综合久久AV一区二区三区| 久久996热精品xxxx| 久久线看观看精品香蕉国产| 国产精品视频久久久| 99久久超碰中文字幕伊人| 久久人人爽人人爽人人片AV不| 久久丫精品国产亚洲av不卡| 亚洲精品综合久久| 久久天天婷婷五月俺也去| 亚洲婷婷国产精品电影人久久| 欧美久久久久久精选9999| 日韩电影久久久被窝网| 香蕉久久夜色精品国产尤物| 狠狠色狠狠色综合久久|