zoukankan      html  css  js  c++  java
  • POJ --- 2676 Sudoku

    Sudoku
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12818   Accepted: 6381   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

    思路:从左向右一个个处理,DFS





     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int map[20][20], cnt[10];
     6 char str[10][10];
     7 bool Judge(int x, int y, int num){
     8     for(int i = 1;i <= 9;i ++){
     9         if(map[x][i] == num || map[i][y] == num)
    10             return false;
    11     }
    12     int row = 1 + ((x - 1) / 3)*3;
    13     int col = 1 + ((y - 1) / 3)*3;
    14     for(int i = row;i < row + 3;i ++){
    15         for(int j = col;j < col + 3;j ++)
    16             if(map[i][j] == num) return false;
    17     }
    18     return true;
    19 }
    20 bool dfs(int x, int y){
    21     if(y == 10){
    22         y = 1;
    23         x ++;
    24         if(x == 10) return true;
    25     }
    26     while(map[x][y] != 0){
    27         y ++;
    28         if(y == 10){
    29             y = 1;
    30             x ++;
    31         }
    32         if(x == 10) return true;
    33     }
    34     for(int i = 1;i <= 9;i ++){
    35         if(Judge(x, y, i)){
    36             map[x][y] = i;
    37             if(dfs(x, y+1)) return true;
    38             map[x][y] = 0;
    39         }
    40     }
    41     return false;
    42 }
    43 int main(){
    44     int T;
    45     //freopen("in.c", "r", stdin);
    46     scanf("%d", &T);
    47     while(T--){
    48         memset(map, -1, sizeof(map));
    49         for(int i = 0;i < 9;i ++){
    50             scanf("%s", str[i]);
    51         }
    52         for(int i = 0;i < 9;i ++){
    53             for(int j = 0;j < 9;j ++){
    54                 map[i+1][j+1] =  str[i][j] - '0';
    55             }
    56         }
    57         dfs(1, 1);
    58         for(int i = 1;i <= 9;i ++){
    59             for(int j = 1;j <= 9;j ++)
    60                 printf("%d", map[i][j]);
    61             printf("
    ");
    62         }
    63     }
    64     return 0;
    65 }
    
    
  • 相关阅读:
    对象的属性定义
    webpack中loader和plugins
    数据的去重方法
    将本地项目上传到github
    Lucene全文检索
    golang操作Rabbit
    Mysql主从同步
    MySQL回顾
    Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.4.26.v20200117:run (default-cli) on project ssm-mybatis-plus: Failure
    maven资源导出问题
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3616530.html
Copyright © 2011-2022 走看看