Poj (2676) -- Sudoku

Source: Internet
Author: User

Poj (2676) -- Sudoku

 

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 shocould 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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127

Oh, it turned out to be a question related to practical applications. Did you make this question and then all the sudoku can be cracked. =. = Drunk

This is a dfs question. I don't know the reason why the exact search and anti-search time will be so much different. Please ask someone here.

First, my idea is: there must be three states.

1. The numbers in the same row are not the same, so we store an array in the same row.

2. The numbers in the same column are not the same, so we store an array in the same column.

3. they are in the same 3*3 grid, so we also use an array to store them. At the beginning, I got stuck and didn't know how to save them, later, I also read a lot of questions on the Internet and found that it was a little difficult to understand. So I went through the direct naked violence, directly to each grid for, and then saved their existing numbers. Brute force strategy ~ Sad...

* The only thing to note here is to be careful when outputting the lower mark in dfs. Then, we will discuss the classification in dfs. First, the current status is 0, when the number is not filled, the number is enumerated and then filled in. When the number is filled, the next enumeration is skipped, NOTE: If it is the last row of the current row, it must be written as dfs (x +), that is, re-start enumeration from the first position of the next row.

 

# Include
 
  
# Include
  
   
# Include
   
    
# Define deusing namespace std; # define maxn 11 char a [maxn] [maxn]; int row [maxn] [maxn], line [maxn] [maxn], grid [maxn] [maxn]; bool ff = false; void dfs (int x, int y) {if (x =-1) {ff = true; return ;} if (a [x] [y]! = '0') {if (y = 0) dfs (x-1, 8); else dfs (x, Y-1); return ;} else if (a [x] [y] = '0') {for (int I = 1; I <= 9; I ++) {if (! Line [y] [I] &! Row [x] [I] &! Grid [x/3*3 + y/3] [I]) {a [x] [y] = I + '0 '; line [y] [I] = 1; row [x] [I] = 1; grid [x/3*3 + y/3] [I] = 1; if (y = 0) dfs (x-1, 8); else dfs (x, Y-1); if (ff) return; line [y] [I] = 0; row [x] [I] = 0; grid [x/3*3 + y/3] [I] = 0; a [x] [y] = '0' ;}}} int main () {int T; scanf ("% d", & T); while (T --) {memset (row, 0, sizeof (row); memset (line, 0, sizeof (line); memset (grid, 0, sizeof (grid )); for (int I = 0; I <9; I ++) scanf ("% s", a [I]); // The following are the numbers that have occurred in each row. for (int I = 0; I <9; I ++ ){ For (int j = 0; j <9; j ++) {if (a [I] [j]-'0 '! = 0) row [I] [a [I] [j]-'0'] = 1 ;}/// the following are the numbers used to judge the occurrence of each column; for (int j = 0; j <9; j ++) {for (int I = 0; I <9; I ++) {if (a [I] [j]-'0 '! = 0) line [j] [a [I] [j]-'0'] = 1; // j indicates the number of columns;} // brute-force lattice search; for (int I = 0; I <3; I ++) for (int j = 0; j <3; j ++) if (a [I] [j]-'0 '! = 0) grid [0] [a [I] [j]-'0'] = 1; for (int I = 0; I <3; I ++) for (int j = 3; j <6; j ++) if (a [I] [j]-'0 '! = 0) grid [1] [a [I] [j]-'0'] = 1; for (int I = 0; I <3; I ++) for (int j = 6; j <9; j ++) if (a [I] [j]-'0 '! = 0) grid [2] [a [I] [j]-'0'] = 1; for (int I = 3; I <6; I ++) for (int j = 0; j <3; j ++) if (a [I] [j]-'0 '! = 0) grid [3] [a [I] [j]-'0'] = 1; for (int I = 3; I <6; I ++) for (int j = 3; j <6; j ++) if (a [I] [j]-'0 '! = 0) grid [4] [a [I] [j]-'0'] = 1; for (int I = 3; I <6; I ++) for (int j = 6; j <9; j ++) if (a [I] [j]-'0 '! = 0) grid [5] [a [I] [j]-'0'] = 1; for (int I = 6; I <9; I ++) for (int j = 0; j <3; j ++) if (a [I] [j]-'0 '! = 0) grid [6] [a [I] [j]-'0'] = 1; for (int I = 6; I <9; I ++) for (int j = 3; j <6; j ++) if (a [I] [j]-'0 '! = 0) grid [7] [a [I] [j]-'0'] = 1; for (int I = 6; I <9; I ++) for (int j = 6; j <9; j ++) if (a [I] [j]-'0 '! = 0) grid [8] [a [I] [j]-'0'] = 1; ff = false; dfs (8, 8); for (int I = 0; I <9; I ++) {printf ("% s \ n", a [I]) ;}}
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.