POJ2676 Sudoku

本文介绍了一种解决Sudoku(数独)问题的算法实现,通过深度优先搜索(DFS)策略来填充空缺的单元格,确保每行、每列及每个3x3的小方格内的数字1到9各出现一次。

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

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

 
【题解】
爆搜题,本来我的参数里记录了剩余个数,然后愉快炸掉。。后来去了这个参数,突然飞凯过了这个题。。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 
 7 inline void read(int &x)
 8 {
 9     x = 0;char ch = getchar(),c = ch;
10     while(ch < '0' || ch > '9')  c = ch, ch = getchar();
11     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
12     if(c == '-')x = -x;
13 }
14 
15 int num[10][10], col[10][10], row[10][10], ge[10][10],ok[10][10],flag;
16 char ch[10];
17 
18 //还剩下cnt个数可以填 
19 int dfs(int x, int y)
20 {
21     register int pp;
22     if(x == 10)return 1;
23     if(num[x][y])
24     {
25         if(y == 9)flag = dfs(x + 1, 1);
26         else flag = dfs(x, y + 1);
27         if(flag)return 1;
28         else return 0;    
29     }
30     int i = x, j = y,p,q;
31     pp = (i - 1)/3 * 3 + (j + 2)/3;
32     for(register int t = 1;t <= 9;++ t)
33     {
34         if(row[i][t] || col[j][t] || ge[pp][t])continue;
35         row[i][t] = col[j][t] = ge[pp][t] = 1;
36         num[i][j] = t;
37         p = i, q = j;
38         if(q == 9)q = 0, ++ p;
39         flag = dfs(p, q + 1);
40         if(flag)return 1;
41         num[i][j] = 0, col[j][t] = row[i][t] = ge[pp][t] = 0;
42     }
43     return 0;
44 }
45 
46 int main()
47 {
48     register int tmp = 0, t;
49     read(t);
50     for(;t;--t)
51     {
52         memset(col, 0, sizeof(col));
53         memset(row, 0, sizeof(row));
54         memset(ge, 0, sizeof(ge));
55         for(int i = 1;i <= 9;++ i)
56         {
57             scanf("%s", ch + 1);
58             for(register int j = 1;j <= 9;++ j)
59             {
60                 num[i][j] = ch[j] - '0';
61                 if(num[i][j])
62                     ++ tmp,row[i][num[i][j]] = col[j][num[i][j]] = ge[(i - 1)/3 * 3 + (j + 2)/3][num[i][j]] = 1;
63             }
64         }
65         dfs(1,1);
66         for(int i = 1;i <= 9;++ i)
67         {
68             for(register int j = 1;j <= 9;++ j)
69                 printf("%d", num[i][j]);
70             putchar('\n');
71         }
72     }
73     return 0;
74 }
POJ2676 Sudoku

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7354535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值