POJ 2676 Sudoku (数独 DFS)

 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14368   Accepted: 7102   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

Source

 
http://bailian.openjudge.cn/practice/2982/
 
题目大意:就是数独咯,让你求解数独游戏,9乘9的矩阵要求每行每列和9个3*3的子矩阵内都出现数字1-9
 
题目分析:9乘9的矩阵,从第一个位置一直搜到最后一个位置,若当前位置有数字搜下一位置,否则枚举1-9并判断,
判断时当前行r = n/9当前列为c = n%9当前子矩阵的第一个元素位置为r / 3 * 3,c / 3 * 3
复制代码
 1 #include <cstdio>  
 2 char s[10];  
 3 int num[9][9];  
 4 bool flag;  
 5   
 6 bool ok(int n, int cur)   
 7 {  
 8     int r = n / 9;      //当前行  
 9     int c = n % 9;      //当前列  
10     for(int j = 0; j < 9; j++)  //枚举列  
11         if (num[r][j] == cur)   
12             return false;  
13     for(int i = 0; i < 9; i++)  //枚举行  
14         if (num[i][c] == cur)   
15             return false;  
16     //得到当前所在的子矩阵的第一个元素位置  
17     int x = r / 3 * 3;    
18     int y = c / 3 * 3;  
19     //枚举子矩阵中的元素  
20     for(int i = x; i < x + 3; i++)  
21         for(int j = y; j < y + 3; j++)  
22             if (num[i][j] == cur)   
23                 return false;  
24     return true;  
25 }  
26   
27 void DFS(int n)  
28 {  
29     if(n > 80 || flag)   
30     {  
31         flag = true;  
32         return;  
33     }  
34     if(num[n / 9][n % 9])//当前位置有数字直接搜索下一位  
35     {  
36         DFS(n + 1);  
37         if(flag)  
38             return;  
39     }  
40     else  
41     {  
42         for(int cur = 1; cur <= 9; cur++)  //枚举数字  
43         {  
44             if(ok(n, cur)) //若ok则插入  
45             {  
46                 num[n / 9][n % 9] = cur;   
47                 DFS(n + 1);  
48                 if(flag)   
49                     return;  
50                 num[n / 9][n % 9] = 0; //还原  
51             }  
52         }  
53     }  
54 }  
55   
56 int main()  
57 {     
58     int T;  
59     scanf("%d", &T);  
60     while(T--)  
61     {  
62         flag = false;  
63         for(int i = 0; i < 9; i++) //得到数独矩阵  
64         {  
65             scanf("%s", s);  
66             for(int j = 0; j < 9; j++)  
67                 num[i][j] = (s[j] - '0');  
68         }  
69         DFS(0);  //从第一位开始搜  
70         for(int i = 0; i < 9; i++)  
71         {  
72             for(int j = 0; j < 9; j++)  
73                 printf("%d", num[i][j]);  
74             printf("\n");        
75         }  
76     }  
77 }  
View Code
复制代码

题解来源:http://blog.csdn.net/tc_to_top/article/details/43699047 

 

posted on   华山青竹  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示