zoukankan      html  css  js  c++  java
  • Sudoku(回溯)

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12075   Accepted: 6026   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
    题意:数独问题,当有多种方案时任意输出一种;

    思路:回溯法,用三个数组row[][],col[][],square[][]维护某个数是否能被填充,在未填充的空格里尝试着放一个数,继续递归,当发生冲突时就回溯,更改已做的标记;
     1 #include<stdio.h>
     2 #include<string.h>
     3 int map[10][10];
     4 bool row[10][10];//row[i][x] 表示第i行x是否出现过;
     5 bool col[10][10];//col[j][x] 表示第j行x是否出现过;
     6 bool square[10][10];//square[k][x] 表示第k个方格x是否出现过;
     7 
     8 bool dfs(int x, int y)
     9 {
    10 
    11     if(x == 10)
    12         return true;//递归边界;
    13     bool flag = false;
    14 
    15     if(map[x][y])//如果map[x][y]已经填了数字,确定向下递归的方向;
    16     {
    17         if(y == 9)
    18             flag = dfs(x+1,1);
    19         else
    20             flag = dfs(x,y+1);
    21 
    22         if(flag)
    23             return true;
    24         else return false;
    25     }
    26     else
    27     {
    28         int k = 3*((x-1)/3) + (y-1)/3+1;
    29         for(int i = 1; i <= 9; i++)
    30         {
    31             if(!row[x][i] && !col[y][i] && !square[k][i])
    32             {
    33                 map[x][y] = i;//找到合适的i填充;
    34 
    35                 row[x][i] = true;
    36                 col[y][i] = true;
    37                 square[k][i] = true;
    38                 //继续递归
    39                 if(y == 9)
    40                     flag = dfs(x+1,1);
    41                 else flag = dfs(x,y+1);
    42 
    43                 if(flag)
    44                     return true;
    45                 else //回溯,修改已作的标记
    46                 {
    47                     map[x][y] = 0;
    48 
    49                     row[x][i] = false;
    50                     col[y][i] = false;
    51                     square[k][i] = false;
    52                 }
    53             }
    54         }
    55     }
    56     return false;
    57 }
    58 int main()
    59 {
    60     int t;
    61     char s[10];
    62     scanf("%d",&t);
    63     while(t--)
    64     {
    65         memset(row,false,sizeof(row));
    66         memset(col,false,sizeof(col));
    67         memset(square,false,sizeof(square));
    68         for(int i = 1; i <= 9; i++)
    69         {
    70             scanf("%s",s);
    71             for(int j = 0; j < 9; j++)
    72             {
    73                 map[i][j+1] = s[j]-'0';
    74                 if(map[i][j+1])
    75                 {
    76                     int k = 3*((i-1)/3) + j/3+1;
    77                     row[i][ map[i][j+1] ] = true;
    78                     col[j+1][ map[i][j+1] ] = true;
    79                     square[k][ map[i][j+1] ] = true;
    80                 }
    81             }
    82         }
    83         dfs(1,1);
    84         for(int i = 1; i <= 9; i++)
    85         {
    86             for(int j = 1; j <= 9; j++)
    87                 printf("%d",map[i][j]);
    88             printf("
    ");
    89         }
    90     }
    91     return 0;
    92 }
    View Code
  • 相关阅读:
    Django 三(路由)
    Django 二
    Django基础知识
    jquery值,属性,类的操作 文档的操作
    jquery事件对象 , 链式编程,选择器和过滤器,动画;js和jquery互转
    MySQL-1-简介-安装流程
    前端 里的面向对象
    篇二:JS身份证校验
    HTML5属性--(capture="camera") 上传照片或者打开手机相机
    SpringMVC @RequestBody接收Json对象字符串
  • 原文地址:https://www.cnblogs.com/LK1994/p/3273010.html
Copyright © 2011-2022 走看看