• <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>

            我希望你是我獨家記憶

            一段永遠封存的記憶,隨風而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

            USACO——443——(拓撲排序)

            Posted on 2008-08-16 23:02 Hero 閱讀(182) 評論(0)  編輯 收藏 引用 所屬分類: 代碼如詩--ACM
            /*
            ID: wangzha4
            LANG: C++
            TASK: frameup
            */
            //JUDGE_ID: 65448BI
            /*

               Test 1: TEST OK [0.000 secs, 3312 KB]
               Test 2: TEST OK [0.000 secs, 3312 KB]
               Test 3: TEST OK [0.011 secs, 3312 KB]
               Test 4: TEST OK [0.000 secs, 3308 KB]
               Test 5: TEST OK [0.000 secs, 3444 KB]
               Test 6: TEST OK [0.000 secs, 3308 KB]
               Test 7: TEST OK [0.000 secs, 3308 KB]
               Test 8: TEST OK [0.011 secs, 3440 KB]
               Test 9: TEST OK [0.043 secs, 3440 KB]
            */
            //拓撲排序的題目--輸出所有可能的拓撲排序--按字典序
            /*

            具體思路 :
            1. 讀入數據,用used[]記錄那些字符被使用過,并將字符hash到
               從小到大的數字cnt,用mymap[]數組來將數字hash回相應的字符
            2. 尋找矩形框--找到每種字符最大和最小的行列,存放在node[]數組中

            3. 構建圖形edge[][]--掃描輸入,對于每個字母,按照矩形框掃描四個邊
               如果掃描到不相同的字母,建立一條有向邊,edge[][]=1
            4. DFS_Topsort()--對于已經建立好的有向邊利用深度優先搜索排序
            5. 輸出所有的拓撲序列
            */
            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>
            #include 
            <ctype.h>
            #include 
            <math.h>

            #define unllong unsigned long long 
            #define unint unsigned int
            #define printline  printf( "\n" ) 
            typedef 
            long long llong ;
            //const double PI = 2.0 * acos( 0.0 ) ;

            const int Base=1000000000;
            const int Capacity=100;
            const int INF = 1000000 ;
            const int size = 35 ;

            struct NODE
            {
                
            int minr, minc ;
                
            int maxr, maxc ;
            };
            struct NODE node[28] ;

            char data[size][size] ;

            int used[200] ;//把字符映射成數字
            char mymap[200] ;//把數字映射成字符
            int cnt ;//記錄映射的最大數字--圖的最大頂點標號

            int edge[30][30] ;//構建有向圖
            int indeg[30] ;//記錄所有的點的入度

            int row, col ;

            char topout[30] ;//暫存生成的拓撲序列

            struct OUT 
            {
                
            char str[30] ;
            };
            struct OUT out[20000] ;
            int ct_out ;

            int fmax( int a, int b )
            {
                
            if( a > b )    return a ;
                
            else        return b ;
            }

            int fmin( int a, int b )
            {
                
            if( a < b )    return a ;
                
            else        return b ;
            }

            void input()
            {
                memset( used, 
            0sizeof(used) ) ;
                memset( mymap, 
            0sizeof(mymap) ) ;
                memset( indeg, 
            0sizeof(indeg) ) ;
                memset( edge, 
            0sizeof(edge) ) ;

                cnt 
            = 1 ;
                
            forint i=0; i<row; i++ ) { 
                    scanf( 
            "%s", data[i] ) ;
                    
            forint j=0; j<col; j++ ) {
                        
            if'.' == data[i][j] ) continue ;
                        
            if( used[data[i][j]] )    continue ;
                        used[data[i][j]] 
            = cnt ; mymap[cnt++= data[i][j] ;
                    }
            //建立相互映射
                }

                
            forint i=0; i<=26; i++ ) {
                    node[i].minr 
            = node[i].minc = INF ;
                    node[i].maxr 
            = node[i].maxc = -1 ;
                }
            //初始化每個字母的最小最大行列
            }

            void find_degree()
            {
                
            forint i=1; i<cnt; i++ )
                    
            forint j=1; j<cnt; j++ )    
                        indeg[i] 
            += edge[j][i] ;
            }

            void DFS_Topsort( int sn, int tdep )
            {
                
            int back[30] ;//深搜的用于暫存狀態的數組
                forint i=1; i<cnt; i++ ) back[i] = indeg[i] ;

                
            if( sn == tdep )
                {
                    
            //printf( "=======%s\n", topout ) ;
                    strcpy( out[ct_out].str, topout ) ;
                    ct_out
            ++ ;
                    
            return ;
                }
                
            forint i=1; i<cnt; i++ )
                {
                    
            if0 == indeg[i] ) { 
                        topout[sn] 
            = mymap[i] ; indeg[i] = -1 ; 
                        
            forint k=1; k<cnt; k++ ) 
                            
            if( edge[i][k] ) indeg[k]-- ;//入度減1

                        DFS_Topsort( sn
            +1, tdep ) ;

                        
            forint k=1; k<cnt; k++ ) indeg[k] = back[k] ;
                    }
                }
            }

            void find_edge( int curn )
            {
            //對四個邊進行查找--尋找矩形框
                int minr = node[curn].minr ; int minc = node[curn].minc ;
                
            int maxr = node[curn].maxr ; int maxc = node[curn].maxc ;

                
            int nodex, nodey ; nodey = used[curn+'A'] ;
                
            forint k=minc; k<=maxc; k++ )
                {
                    
            if( data[minr][k]!='.'&&data[minr][k]!=curn+'A' ) {
                        nodex 
            = used[data[minr][k]] ; edge[nodey][nodex] = 1 ;
                    }
                    
            if( data[maxr][k]!='.'&&data[maxr][k]!=curn+'A' ) {
                        nodex 
            = used[data[maxr][k]] ; edge[nodey][nodex] = 1 ;
                    }
                }
                
            forint k=minr; k<=maxr; k++ )
                {
                    
            if( data[k][minc]!='.'&&data[k][minc]!=curn+'A' ) {
                        nodex 
            = used[data[k][minc]] ; edge[nodey][nodex] = 1 ;
                    }
                    
            if( data[k][maxc]!='.'&&data[k][maxc]!=curn+'A' ) {
                        nodex 
            = used[data[k][maxc]] ; edge[nodey][nodex] = 1 ;
                    }
                }
            }

            void build_graph()
            {
                
            forint i=0; i<row; i++ ) {
                    
            forint j=0; j<col; j++ ) {
                        
            if'.' == data[i][j] )    continue ;
                        find_edge( data[i][j] 
            - 'A' ) ;
                    }
                }
                
            forint i=1; i<cnt; i++ ) edge[i][i] = 0 ;
            }

            void process()
            {
                
            int curnode ;
                
            forint i=0; i<row; i++ ) {
                    
            forint j=0; j<col; j++ ) {
                        
            if( data[i][j] != '.' ) {
                            curnode 
            = data[i][j] - 'A' ;
                            node[curnode].minr 
            = fmin( node[curnode].minr, i ) ;
                            node[curnode].minc 
            = fmin( node[curnode].minc, j ) ;
                            node[curnode].maxr 
            = fmax( node[curnode].maxr, i ) ;
                            node[curnode].maxc 
            = fmax( node[curnode].maxc, j ) ;
                        }
                    }
                }
            //尋找每個字母的矩形框

                build_graph() ; 
            //建圖

                find_degree() ;
            //計算入度

                ct_out 
            = 0 ;
                DFS_Topsort( 
            0, cnt-1 ) ;//拓撲排序
            }

            int cmp( const void *a, const void *b )
            {
                
            struct OUT *= (struct OUT *)a ;
                
            struct OUT *= (struct OUT *)b ;

                
            return strcmp( c->str, d->str ) ;
            }

            void output()
            {
                qsort( 
            out, ct_out, sizeof(out[1]), cmp ) ;

                
            forint i=0; i<ct_out; i++ )
                {
                    printf( 
            "%s\n"out[i].str ) ;
                }
            }

            int main()
            {
                freopen( 
            "frameup.in""r", stdin ) ;
                freopen( 
            "frameup.out","w",stdout ) ;

                
            //freopen( "in.txt", "r", stdin ) ;

                
            while( scanf( "%d %d"&row, &col ) != EOF )
                {
                    input() ;

                    process() ;

                    output() ;
                }
                
            return 0 ;
            }
            久久久久人妻一区精品果冻| 亚洲国产天堂久久综合| 99久久免费国产特黄| 色婷婷综合久久久久中文| 久久精品一本到99热免费| 1000部精品久久久久久久久| 天天久久狠狠色综合| 久久精品国产亚洲AV不卡| 麻豆av久久av盛宴av| 久久久久亚洲AV片无码下载蜜桃| 久久久久久狠狠丁香| 亚洲国产日韩欧美久久| 日韩精品无码久久久久久| 亚洲国产精品一区二区久久| 久久毛片免费看一区二区三区| 精品久久久久成人码免费动漫| 久久亚洲精品成人AV| 曰曰摸天天摸人人看久久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 99久久国产综合精品成人影院| 久久亚洲高清综合| 久久精品天天中文字幕人妻 | 996久久国产精品线观看| 成人a毛片久久免费播放| 久久午夜福利无码1000合集| 国产精品久久久久aaaa| 无码国内精品久久人妻麻豆按摩| 日本人妻丰满熟妇久久久久久| 91久久香蕉国产熟女线看| 久久久SS麻豆欧美国产日韩| 久久精品一区二区国产| 久久久久亚洲av成人网人人软件 | 少妇熟女久久综合网色欲| 久久久综合九色合综国产| 久久受www免费人成_看片中文| 久久精品中文騷妇女内射| 日本加勒比久久精品| 久久久久久狠狠丁香| 亚洲αv久久久噜噜噜噜噜| 久久精品国产精品亚洲| 久久精品国产99久久久|