• poj 2676 -- Sudoku


    Sudoku
     
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13723   Accepted: 6791   Special Judge

    Description

    Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

    Input

    The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

    Output

    For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

    Sample Input

    1
    103000509
    002109400
    000704000
    300502006
    060000050
    700803004
    000401000
    009205800
    804000107

    Sample Output

    143628579
    572139468
    986754231
    391542786
    468917352
    725863914
    237481695
    619275843
    854396127

    思路:这个题正着搜超时,倒着搜16ms

     1 /*======================================================================
     2  *           Author :   kevin
     3  *         Filename :   suduku.cpp
     4  *       Creat time :   2014-08-06 15:51
     5  *      Description :
     6  ========================================================================*/
     7 #include <iostream>
     8 #include <algorithm>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <queue>
    12 #include <cmath>
    13 #define clr(a,b) memset(a,b,sizeof(a))
    14 #define M 20
    15 using namespace std;
    16 int s[M][M],row[M][M],col[M][M];
    17 /*-------判断是否有3×3格子里是否有b-------*/
    18 bool judge(int x,int y,int b)
    19 {
    20     int i,j;
    21     for(i = (x-1)/3*3+1; i <= (x-1)/3*3+3; i++){
    22         for(j = (y-1)/3*3+1; j <= (y-1)/3*3+3; j++){
    23             if(s[i][j] == b) return false;
    24         }
    25     }
    26     return true;
    27 }
    28 /*----------------end----------------*/
    29 bool DFS(int x,int y) // x代表列,y代表行
    30 {
    31     if(x == 9 && y == 0){
    32         return true;
    33     }
    34     if(s[y][x]){
    35         if(x > 1){
    36             if(DFS(x-1,y))
    37                 return true;
    38         }
    39         if(x == 1){
    40             if(DFS(9,y-1))
    41                 return true;
    42         }
    43     }
    44     else{
    45         for(int i = 1; i <= 9; i++){      //枚举1到9
    46             if(!row[y][i] && !col[x][i]){ //当前行当前列是否有i
    47                 if(judge(y,x,i)){         //判断能否放在3×3方格里
    48                     row[y][i] = 1; col[x][i] = 1;
    49                     s[y][x] = i;
    50                     if(x > 1){
    51                         if(DFS(x-1,y)){
    52                             return true;
    53                         }
    54                     }
    55                     if(x == 1){
    56                         if(DFS(9,y-1)){
    57                             return true;
    58                         }
    59                     }
    60                     row[y][i] = 0; col[x][i] = 0;
    61                     s[y][x] = 0;
    62                 }
    63             }
    64         }
    65     }
    66     return false;
    67 }
    68 int main(int argc,char *argv[])
    69 {
    70     int n;
    71     scanf("%d",&n);
    72     getchar();
    73     while(n--){
    74         clr(s,0);
    75         clr(row,0);
    76         clr(col,0);
    77         char c;
    78         for(int i  = 1; i <= 9; i++){
    79             for(int j = 1; j <= 9; j++){
    80                 scanf("%c",&c);
    81                 s[i][j] = c-'0';
    82                 row[i][s[i][j]] = 1;   //标记行里出现的数
    83                 col[j][s[i][j]] = 1;   //标记列里出现的数
    84             }
    85             getchar();
    86         }
    87         DFS(9,9);          //倒着搜
    88         for(int i = 1; i <= 9; i++){
    89             for(int j = 1; j <= 9; j++){
    90                 printf("%d",s[i][j]);
    91             }
    92             printf("
    ");
    93         }
    94     }
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    NodeJS Stream 五:双工流
    NodeJS Stream 四:Writable
    NodeJS Stream 三:readable
    NodeJS Stream 二:什么是 Stream
    NodeJS stream 一:Buffer
    JavaScript 精粹
    详解k8s中的liveness和readiness的原理和区别_转
    通过 GoLang 全面了解 HTTPS_转自知乎-腾讯技术
    golang之vim和vscode开发环境
    go命令工具补遗
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3895603.html
Copyright © 2020-2023  润新知