poj 2676 -- Sudoku

本文介绍了一种解决数独游戏的方法,通过逆向搜索算法实现了数独的自动求解。详细阐述了输入数据格式、输出结果形式以及解题过程中的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("\n");
93         }
94     }
95     return 0;
96 }
View Code

 

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3895603.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值