• POJ --- 2918 求解数独


    Tudoku
     

    Description

    Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at our university and teaches newcomers like Jim. In the first lesson he explained the game of Tudoku to Jim. Tudoku is a straight-forward variant of Sudoku, because it consists of a board where almost all the numbers are already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of being too lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom left for him.

    Sudoku is played on a 9 × 9 board that is divided into nine different 3 × 3 blocks. Initially, it contains only a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 × 3 block contains every number from 1 to 9. This can be quite hard but remember that Tom already filled most cells. A resulting Tudoku board can be solved using the following rule repeatedly: if some row, column or 3 × 3 block contains exactly eight numbers, fill in the remaining one.

    In the following example, three cells are still missing. The upper left one cannot be determined directly because neither in its row, column, or block, there are eight numbers present. The missing number for the right cell can be determined using the above rule, however, because its column contains exactly eight numbers. Similarly, the number for the lower-most free cell can be determined by examining its row. Finally, the last free cell can be filled by either looking at its row, column or block.

    7 5 3 2 8 4 6 9 1
    4 8 2 9 1 6 5 3 7
    1 9 6 7 5 3 8 4 2
    9 3 1   6   4 2 5
    2 7 5 4 9 1 3 8 6
    6 4 8   3 2 1 7 9
    5 6 7 3 4 9 2 1 8
    8 2 4 1 7 5 9 6 3
    3 1 9 6 2 8 7 5 4

    Input

    The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digits each. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenario is terminated by an empty line.

    Output

    The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for the input, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with a blank line.

    Sample Input

    2
    000000000
    817965430
    652743190
    175439820
    308102950
    294856370
    581697240
    903504610
    746321580
    
    781654392
    962837154
    543219786
    439182675
    158976423
    627543918
    316728549
    895461237
    274395861

    Sample Output

    Scenario #1:
    439218765
    817965432
    652743198
    175439826
    368172954
    294856371
    581697243
    923584617
    746321589
    
    Scenario #2:
    781654392
    962837154
    543219786
    439182675
    158976423
    627543918
    316728549
    895461237
    274395861

    思路:dfs,试填每个方格,当搜索的范围超过9×9时说明已经找到解。以前感觉挺难的,没敢写,今天下午写了一下感觉并不难,一次AC。

     1 #include<cstdio>
     2 #include<string>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std; 
     7 int map[15][15], flag; 
     8 bool CanPlace(int x, int y, int num){
     9     for(int i = 1; i <= 10; i ++) 
    10         if(map[x][i] == num || map[i][y] == num) return false; 
    11     int row = ((x-1)/3)*3+1; 
    12     int col = ((y-1)/3)*3+1; 
    13     for(int i = row; i < row+3; i ++){
    14         for(int j = col;j < col+3; j ++ )
    15             if(map[i][j] == num) return false; 
    16     }
    17     return true; 
    18 }
    19 void dfs(int x, int y){
    20     if(x == 9 && y > 9){
    21         flag = 1; 
    22         for(int i = 1; i < 10; i ++){
    23             for(int j = 1; j < 10; j ++) printf("%d", map[i][j]); 
    24             printf("
    "); 
    25         }
    26         return; 
    27     }
    28     if(y > 9){
    29         x++; 
    30         y = 1; 
    31     }
    32     if(!map[x][y]){
    33         for(int i = 1;i < 10; i ++){
    34             if(CanPlace(x, y, i)){
    35                 map[x][y] = i; 
    36                 dfs(x, y+1); 
    37                 if(flag) return; 
    38                 map[x][y] = 0; 
    39             }
    40         }
    41     }else dfs(x, y+1); 
    42 }
    43 int main(){
    44     char str[11]; 
    45     int t,cnt = 0; 
    46     //freopen("in.c", "r", stdin); 
    47     scanf("%d", &t); 
    48     while(t--){
    49         printf("Scenario #%d:
    ", ++cnt); 
    50         memset(str, 0, sizeof(str)); 
    51         for(int i = 0; i < 9; i ++){
    52             scanf("%s", str); 
    53             for(int j = 0; j < 9; j ++){
    54                 map[i+1][j+1] = str[j]-'0'; 
    55             }
    56         }
    57         flag = 0; 
    58         dfs(1, 1); 
    59         puts(""); 
    60     }
    61     return 0; 
    62 }

  • 相关阅读:
    3.15第三周编程总结
    2019.3.9编程总结
    2019.3.3编程总结2
    编程总结1
    编程总结2
    编程总结3
    我的老师
    关于sublime text 3使用记录
    12. 整数转罗马数字
    4. 寻找两个有序数组的中位数
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3686065.html
Copyright © 2020-2023  润新知