Poj Sudoku (Sudoku) DFS

Source: Internet
Author: User
Sudoku
Time limit:2000 ms   Memory limit:65536 K
Total submissions:13665   Accepted:6767   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 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
Explanation: games have been played, but they have never been made. Haha, haha, and interesting code. I know the meaning of the question. The key is how to search, in fact, it is constantly enumerating and jumping out if all the conditions are met;
AC code:
1 # include <iostream> 2 # include <algorithm> 3 # include <cstdio> 4 # include <cstring> 5 using namespace STD; 6 const int n = 11; 7 int map [N] [N], flag; 8 char map [N] [N]; 9 int check (INT ans, int key) 10 {11 int x = (key-1)/9 + 1; 12 INT y = key-(x-1) * 9; 13 for (INT I = 1; I <= 9; I ++) // does the row conflict? 14 if (Map [x] [I] = ans) 15 return 0; 16 for (INT I = 1; I <= 9; I ++) // whether the column conflicts 17 if (Map [I] [Y] = ans) 18 return 0; 19 if (x <= 3) X = 1 ;// The starting point of X is 20 if (x> 3 & x <7) x = 4; 21 if (x> = 7) x = 7; 22 if (Y <= 3) y = 1; // the starting point of the small nine-grid where Y is located. 23 if (Y> 3 & Y <7) y = 4; 24 if (Y> = 7) y = 7; 25 for (INT I = 0; I <3; I ++) // whether the small nine cells CONFLICT 26 for (Int J = 0; j <3; j ++) 27 if (Map [x + I] [Y + J] = ans) 28 return 0; 29 return 1; // If none of the preceding conditions meet, return 1; 30} 31 int DFS (INT key) 32 {33 int x = (key-1)/9 + 1; 34 int y = key-(x-1) * 9; 35 if (Key> 81) 36 {37 flag = 1; 38 return 0; 39} 40 if (Map [x] [Y]! = 0) // if it is not 0, Jump directly to 41 {42 DFS (Key + 1); 43} 44 else // No. In this position, nine numbers are enumerated, perform recursive search for 45 {46 for (int K = 1; k <= 9; k ++) 47 If (check (K, key )) 48 {49 map [x] [Y] = K; 50 DFS (Key + 1); 51 if (flag = 1) return 0; 52 map [x] [Y] = 0; 53} 54} 55 return 0; 56} 57 int main () 58 {59 int T, I, J; 60 // freopen ("in2.txt", "r", stdin); 61 // freopen ("out2.txt", "W", stdout); 62 scanf ("% d ", & T); 63 while (t --) 64 {65 memset (MAP, 0, sizeof (MAP); 66 for (I = 1; I <= 9; I ++) 67 for (j = 1; j <= 9; j ++) 68 {69 CIN> map [I] [J]; 70 map [I] [J] = map [I] [J]-'0'; 71} 72 flag = 0; 73 DFS (1 ); 74 for (I = 1; I <= 9; I ++) 75 {76 for (j = 1; j <= 9; j ++) 77 printf ("% d", map [I] [J]); 78 printf ("\ n"); 79} 80} 81 return 0; 82}

 

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.