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

            bingo

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              0 隨筆 :: 4 文章 :: 1 評論 :: 0 Trackbacks

             

              1//金幣陣列問題,待改進。。。
              2
              3#include <stdio.h> 
              4#include <memory.h> 
              5
              6int ma[100][101], mb[100][101], mc[100][101]; 
              7int steps, tempsteps; 
              8int m, n; 
              9FILE *input, *output; 
             10
             11int input_matrix(int [][]); 
             12int row_overturn(int [][], int); 
             13int row_exchange(int [][], intint); 
             14int row_match(int [][], int [][], int); 
             15int col_match(intintintint); 
             16int col_exchange(intint); 
             17int evaluate(intint); 
             18int matrix_copy(int [][], int [][], intint); 
             19
             20int row_match(int mx[][101], int my[][101], int i) 
             21
             22    int j, count = 0
             23    for (j = 1; j <= n; ++j) 
             24        if (mx[i][j] != my[i][j]) 
             25            ++count; 
             26    if (count == n) 
             27        return 1
             28    else if(!count) 
             29        return 0
             30    else 
             31        return -1
             32}
             
             33
             34int input_matrix(int mx[][101]) 
             35
             36    int i, j, count1; 
             37    int tag = 0
             38    for (i = 0; i < m; ++i) 
             39    
             40        count1 = 0
             41        for (j = 1; j <= n; ++j) 
             42        
             43            fscanf(input, "%d"&mx[i][j]); 
             44            if (mx[i][j]) 
             45                ++count1; 
             46        }
             
             47
             48        if (count1 * 2 != n) 
             49        
             50            mx[i][0= count1; 
             51            if (i != tag) 
             52                row_exchange(mx, i, tag); 
             53            ++tag; 
             54        }
             
             55        else 
             56            mx[i][0= -1
             57    }
             
             58
             59    return tag; 
             60}
             
             61
             62int row_overturn(int mx[][101], int thisrow) 
             63
             64    int j; 
             65
             66    for (j = 1; j <= n; j++
             67        mx[thisrow][j] = mx[thisrow][j] ^ 1
             68    if (mx[thisrow][0>= 0
             69        mx[thisrow][0= n - mx[thisrow][0]; 
             70
             71    ++steps; 
             72
             73    return 0
             74}
             
             75
             76int row_exchange(int mx[][101], int row1, int row2) 
             77
             78    int j, temp; 
             79    for (j = 0; j <= n; ++j) 
             80    
             81        temp = mx[row1][j]; 
             82        mx[row1][j] = mx[row2][j]; 
             83        mx[row2][j] = temp; 
             84    }
             
             85
             86    return 0
             87}
             
             88
             89int col_match(int x, int y, int fromrow, int torow) 
             90
             91    int i; 
             92    for (i = fromrow; i < torow; ++i) 
             93        if (ma[i][x] != mb[i][y]) 
             94            return 0
             95
             96    return 1
             97}
             
             98
             99int col_exchange(int col1, int col2) 
            100
            101    int i, temp; 
            102    for (i = 0; i < m; ++i) 
            103    
            104        temp = ma[i][col1]; 
            105        ma[i][col1] = ma[i][col2]; 
            106        ma[i][col2] = temp; 
            107    }
             
            108    ++steps; 
            109
            110    return 0
            111}
             
            112
            113int matrix_copy(int mx[][101], int my[][101], int rowbound, int colbound) 
            114
            115    int i, j; 
            116    for (i = 0; i < rowbound; ++i) 
            117        for (j = 0; j <= colbound; ++j) 
            118            mx[i][j] = my[i][j]; 
            119
            120    return 0
            121}
             
            122
            123int evaluate(int fromrow, int col) 
            124
            125    int i, degree = 0
            126    for (i = fromrow; i < m; ++i) 
            127        if (ma[i][col] == mb[i][1]) 
            128            ++degree; 
            129
            130    return degree; 
            131}
             
            132
            133int main() 
            134
            135    int c, i, j, j1, from, able, tag1, tag2, step, bestcol = 1, temp; 
            136    int order[101], count[101]; 
            137
            138    output = fopen("output.txt""w"); 
            139    input = fopen("input.txt""r"); 
            140
            141    fscanf(input, "%d"&c); 
            142
            143    for ( ; c; --c) 
            144    
            145        fscanf(input, "%d %d"&m, &n); 
            146        tag1 = input_matrix(ma); 
            147        tag2 = input_matrix(mb); 
            148
            149        if (tag1 != tag2) 
            150        
            151            fprintf(output, "-1\n"); 
            152            continue
            153        }
             
            154        else 
            155        
            156            //將 1,0個數相等的行往下沉 
            157            for (i = 0, able = 1; i < tag1; ++i) 
            158            
            159                if (ma[i][0== mb[i][0]) 
            160                    continue
            161                else if (ma[i][0== n - mb[i][0]) 
            162                    row_overturn(ma, i); 
            163                else 
            164                
            165                    fprintf(output, "-1\n"); 
            166                    able = 0
            167                    goto loop1; 
            168                }
             
            169            }
             
            170            loop1: 
            171                if (!able) 
            172                    continue
            173
            174            //排列 1,0個數相等的行之上的所有列 
            175            for (j = 1, from = 1; j <= n; ++j) 
            176            
            177                for (j1 = from, able = 0; j1 <= n; ++j1) 
            178                    if (col_match(j1, j, 0, tag1)) 
            179                    
            180                        if (j1 != j) 
            181                            col_exchange(j1, j); 
            182                        ++from; 
            183                        able = 1
            184                        break
            185                    }
             
            186
            187                if (!able) 
            188                
            189                    fprintf(output, "-1\n"); 
            190                    goto loop2; 
            191                }
             
            192            }
             
            193            loop2: 
            194                if (!able) 
            195                       continue
            196
            197            if (tag1 == m) 
            198            
            199                fprintf(output, "%d\n", steps); 
            200                continue
            201            }
             
            202
            203            //處理剩下 0,1個數相等的行 
            204            for (j = 1; j <= n; ++j) 
            205            
            206                order[j] = j; 
            207                count[j] = evaluate(tag1, j); 
            208            }
             
            209
            210            for (i = 1; i < n; ++i) 
            211                for (j = 1; j <= n - i; ++j) 
            212                    if (count[j] < count[j + 1]) 
            213                    
            214                        temp = order[j]; 
            215                        order[j] = order[j + 1]; 
            216                        order[j + 1= temp; 
            217                    }
             
            218
            219            /* 
            220            for (j = 1; j <= n; ++j) 
            221                printf("%-2d ", count[order[j]]); 
            222
            223            printf("\n"); 
            224
            225            for (j = 1; j <= n; ++j) 
            226                printf("%-2d ", order[j]); 
            227
            228            printf("\n"); 
            229            */
             
            230
            231            for (i = tag1, able = 1, tempsteps = 0; i < m; ++i) 
            232            
            233                step = row_match(ma, mb, i); 
            234                if (step < 0
            235                
            236                    able = 0
            237                    break
            238                }
             
            239                else if (step) 
            240                    tempsteps += step; 
            241                else 
            242                    ; 
            243            }
             
            244
            245            if (able) 
            246            
            247                steps += tempsteps; 
            248                fprintf(output, "%d\n", steps); 
            249                continue
            250            }
             
            251
            252            tempsteps = steps; 
            253
            254            //找到和目標第一列的最優(yōu)匹配 
            255            loop3: 
            256            steps = tempsteps; 
            257            for (j1 = bestcol, j = order[j1], able = 0; j1 <= n; ++j1) 
            258                if (col_match(j, 10, tag1)) 
            259                
            260                    able = 1
            261                    matrix_copy(mc, ma, m, n); 
            262                     bestcol = j1 + 1
            263                    for (i = tag1; i < m; ++i) 
            264                        if (ma[i][j] != mb[i][1]) 
            265                               row_overturn(ma, i); 
            266                    if (j != 1
            267                        col_exchange(j, 1); 
            268                    break
            269                }
             
            270
            271            if (!able) 
            272            
            273                fprintf(output, "-1\n"); 
            274                continue
            275            }
             
            276
            277               for (j = 2, from = 2; j <= n; ++j) 
            278            
            279                for (j1 = from, able = 0; j1 <= n; ++j1) 
            280                
            281                    if (col_match(j1, j, 0, m)) 
            282                    
            283                        if (j1 != j) 
            284                            col_exchange(j, j1); 
            285                        able = 1
            286                        ++from; 
            287                        break
            288                    }
             
            289                }
             
            290                if (!able) 
            291                    break
            292            }
             
            293
            294            //找到 
            295            if (able) 
            296            
            297                for (i = 0; i < m; ++i) 
            298                
            299                    for (j = 0; j <= n; ++j) 
            300                        printf("%-2d ", ma[i][j]); 
            301                    printf("\n"); 
            302                }
             
            303                printf("steps: %d\n", steps); 
            304                continue
            305            }
             
            306            else if (!able && bestcol == n - 1
            307            
            308                fprintf(output, "-1\n"); 
            309                continue
            310            }
             
            311            else 
            312            
            313                for (i = 0; i < m; ++i) 
            314                
            315                    for (j = 0; j <= n; ++j) 
            316                        printf("%-2d ", mc[i][j]); 
            317                    printf("\n"); 
            318                }
             
            319                matrix_copy(ma, mc, m, n); 
            320                goto loop3; 
            321            }
             
            322        }
             
            323        steps = 0
            324    }
             
            325
            326    /* 
            327    for (i = 0; i < m; ++i) 
            328    { 
            329        for (j = 0; j <= n; ++j) 
            330            printf("%-2d ", ma[i][j]); 
            331        printf("\n"); 
            332    } 
            333
            334    printf("\n"); 
            335
            336    for (i = 0; i < m; ++i) 
            337    { 
            338        for (j = 0; j <= n; ++j) 
            339            printf("%-2d ", mb[i][j]); 
            340        printf("\n"); 
            341    } 
            342    */
             
            343
            344    return 0
            345}

            346
            posted on 2008-12-24 15:26 bingo 閱讀(565) 評論(0)  編輯 收藏 引用
            久久成人小视频| 无码人妻少妇久久中文字幕蜜桃| 午夜精品久久久久久中宇| 青青热久久国产久精品| 国産精品久久久久久久| www.久久热.com| 精品久久久久久中文字幕| 久久久精品2019免费观看| 伊人久久大香线蕉亚洲五月天| 一级做a爰片久久毛片看看| 日日狠狠久久偷偷色综合96蜜桃| 99久久精品久久久久久清纯| 91久久成人免费| 精品视频久久久久| 日本国产精品久久| 国产精品久久久久久久久软件| 色欲综合久久躁天天躁| 久久亚洲国产最新网站| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久国产精品无码网站| 99热都是精品久久久久久| 久久精品国产精品亚洲艾草网美妙| 国产综合成人久久大片91| 亚洲国产精品狼友中文久久久| 中文精品99久久国产 | 成人综合伊人五月婷久久| 久久久久久亚洲精品成人| 欧美久久综合性欧美| 国产精品亚洲美女久久久| 亚洲国产成人乱码精品女人久久久不卡 | 久久综合亚洲色一区二区三区| 久久精品国产亚洲αv忘忧草| 久久国产欧美日韩精品| 国产精自产拍久久久久久蜜| 久久精品桃花综合| 99久久无码一区人妻a黑| 久久国产热这里只有精品| 久久国产劲爆AV内射—百度| 久久精品9988| 国产亚洲精品久久久久秋霞| 99久久精品免费看国产|