码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2676 Sudoku

时间:2017-08-13 20:52:04      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:题解   uniq   std   cells   mis   ima   sof   output   read   

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

 

POJ2676 Sudoku

标签:题解   uniq   std   cells   mis   ima   sof   output   read   

原文地址:http://www.cnblogs.com/huibixiaoxing/p/7354535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!