挺有意思的题(数独)poj2676

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
---------------------------------------------code--------------------------------------------------------------
同一行,同一列,同一个3*3的方格中,不能有重复的数字。
没有加剪枝,一阵爆搜。。。用了位运算判重。
4178209chinaeli2676Accepted204K516MSC++1881B2008-10-05 18:00:13
 
 
  1. #include<iostream>
  2. using namespace std;
  3. int c[9],r[9],s[9];
  4. int board[9][9],sum;
  5. bool flag;
  6. int square ( int m , int n )
  7. {
  8.     if ( m>=0 && m<3 && n>=0 && n<3 )
  9.         return 0;
  10.     if ( m>=0 && m<3 && n>=3 && n<6 )
  11.         return 1;
  12.     if ( m>=0 && m<3 && n>=6 && n<9 )
  13.         return 2;
  14.     if ( m>=3 && m<6 && n>=0 && n<3 )
  15.         return 3;
  16.     if ( m>=3 && m<6 && n>=3 && n<6 )
  17.         return 4;
  18.     if ( m>=3 && m<6 && n>=6 && n<9 )
  19.         return 5;
  20.     if ( m>=6 && m<9 && n>=0 && n<3 )
  21.         return 6;
  22.     if ( m>=6 && m<9 && n>=3 && n<6 )
  23.         return 7;
  24.     if ( m>=6 && m<9 && n>=6 && n<9 )
  25.         return 8;
  26. }
  27. void init ( )
  28. {
  29.     int i,j,k;
  30.     memset(c,0,sizeof(c));
  31.     memset(r,0,sizeof(r));
  32.     memset(s,0,sizeof(s));
  33.     sum=0;
  34.     for ( i=0 ; i<9 ; i++ )
  35.         for ( j=0 ; j<9 ; j++ )
  36.         {
  37.             scanf("%1d",&board[i][j]);
  38.             if ( board[i][j] )
  39.             {
  40.                 r[i]|=(1<<board[i][j]);
  41.                 c[j]|=(1<<board[i][j]);
  42.                 s[square(i,j)]|=(1<<board[i][j]);
  43.             }
  44.             else
  45.                 sum++;
  46.         }
  47.     flag=false;
  48. }
  49. void dfs ( int m , int n , int left )
  50. {
  51.     int i,j;
  52.     if ( m==9 || !left )
  53.     {
  54.         if ( !left )
  55.             flag=true;
  56.         return;
  57.     }
  58.     else
  59.     if ( board[m][n] )
  60.     {
  61.         if ( n==8 )
  62.                 dfs(m+1,0,left);
  63.             else
  64.                 dfs(m,n+1,left);
  65.     }
  66.     else
  67.     {
  68.         for ( i=1 ; i<=9 && !flag ; i++ )
  69.         {
  70.             if ( !((r[m]>>i)&1) && !( (c[n]>>i)&1) && !( (s[square(m,n)]>>i)&1) )
  71.             {
  72.                 r[m]|=1<<i,c[n]|=1<<i;
  73.                 s[square(m,n)]|=1<<i;
  74.                 board[m][n]=i;
  75.                 if ( n==8 )
  76.                     dfs(m+1,0,left-1);
  77.                 else
  78.                     dfs(m,n+1,left-1);
  79.                 if ( !flag )
  80.                 {
  81.                     board[m][n]=0;
  82.                     r[m]^=1<<i,c[n]^=1<<i,s[square(m,n)]^=1<<i;
  83.                 }
  84.                 else
  85.                     return;
  86.             }
  87.         }
  88.     }
  89. }
  90. void print ( )
  91. {
  92.     int i,j;
  93.     for ( i=0 ; i<9 ; i++ )
  94.     {
  95.         for ( j=0 ; j<9 ; j++ )
  96.             printf("%d",board[i][j]);
  97.         printf("/n");
  98.     }
  99. }
  100. int main ( )
  101. {
  102.     int n;
  103.     scanf("%d",&n);
  104.     while ( n-- )
  105.     {
  106.         init();
  107.         dfs(0,0,sum);
  108.         print();
  109.     }
  110. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用shell脚本来解答数独。以下是一种实现方式: 1. 定义数独矩阵 首先需要定义一个9x9的数独矩阵,可以使用二维数组来表示。例如: ``` sudo=( [0]="5 3 0 0 7 0 0 0 0" [1]="6 0 0 1 9 5 0 0 0" [2]="0 9 8 0 0 0 0 6 0" [3]="8 0 0 0 6 0 0 0 3" [4]="4 0 0 8 0 3 0 0 1" [5]="7 0 0 0 2 0 0 0 6" [6]="0 6 0 0 0 0 2 8 0" [7]="0 0 0 4 1 9 0 0 5" [8]="0 0 0 0 8 0 0 7 9" ) ``` 其中,0表示需要填写的空格,其他数字表示已经填写的数字。 2. 实现数独算法 使用shell脚本来实现数独算法。可以使用循环来遍历数独矩阵,对于每个空格,尝试填入1~9的数字,判断是否满足数独的规则(每行、每列、每个3x3宫格中不存在重复数字)。如果满足规则,则继续递归填写下一个空格,如果填写完成,则表示数独已经解答完毕。 以下是一个简单的数独算法实现: ``` #!/bin/bash # 数独矩阵 sudo=( [0]="5 3 0 0 7 0 0 0 0" [1]="6 0 0 1 9 5 0 0 0" [2]="0 9 8 0 0 0 0 6 0" [3]="8 0 0 0 6 0 0 0 3" [4]="4 0 0 8 0 3 0 0 1" [5]="7 0 0 0 2 0 0 0 6" [6]="0 6 0 0 0 0 2 8 0" [7]="0 0 0 4 1 9 0 0 5" [8]="0 0 0 0 8 0 0 7 9" ) # 判断行是否符合规则 function check_row() { row=$1 num=$2 for col in {0..8}; do if [[ ${sudo[$row][$col]} == $num ]]; then return 1 fi done return 0 } # 判断列是否符合规则 function check_col() { col=$1 num=$2 for row in {0..8}; do if [[ ${sudo[$row][$col]} == $num ]]; then return 1 fi done return 0 } # 判断3x3宫格是否符合规则 function check_block() { row=$1 col=$2 num=$3 start_row=$((row / 3 * 3)) start_col=$((col / 3 * 3)) for i in {0..2}; do for j in {0..2}; do if [[ ${sudo[$((start_row+i))][$((start_col+j))]} == $num ]]; then return 1 fi done done return 0 } # 递归填写数独 function solve_sudoku() { for row in {0..8}; do for col in {0..8}; do if [[ ${sudo[$row][$col]} == 0 ]]; then for num in {1..9}; do if check_row $row $num || check_col $col $num || check_block $row $col $num; then continue fi sudo[$row][$col]=$num if solve_sudoku; then return 0 fi sudo[$row][$col]=0 done return 1 fi done done return 0 } # 解答数独 solve_sudoku # 输出结果 for row in {0..8}; do for col in {0..8}; do echo -n "${sudo[$row][$col]} " done echo done ``` 3. 运行脚本 将上述代码保存为sudoku.sh文件,并使用bash命令运行该脚本: ``` bash sudoku.sh ``` 即可得到数独的解答结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值