Poj 2676 -- Sudoku

Source: Internet
Author: User
Sudoku
Time limit:2000 ms   Memory limit:65536 K
Total submissions:13723   Accepted:6791   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
Train of Thought: This question is about search timeout, searching for 16 Ms

1/* ============================================== ====================================== 2 * Author: kevin 3 * filename: suduku. CPP 4 * creat time: 5 * description: 6 ================================================ ===================================== */7 # include <iostream> 8 # include <algorithm> 9 # include <cstdio> 10 # include <cstring> 11 # include <queue> 12 # include <cmath> 13 # define CLR (, b) memset (, B, sizeof (A) 14 # define M 2015 using namespace STD; 16 int s [m] [m], row [m] [m], col [m] [m]; 17/* ------- determine whether there is B in the 3 × 3 lattice ------- */18 bool judge (int x, int y, int B) 19 {20 int I, j; 21 for (I = (x-1)/3*3 + 1; I <= (x-1)/3*3 + 3; I ++) {22 for (j = (Y-1)/3*3 + 1; j <= (Y-1)/3*3 + 3; j ++) {23 if (s [I] [J] = B) Return false; 24} 25} 26 return true; 27} 28/* ---------------- end -------------- */29 bool DFS (int x, int y) // X indicates the column, and y indicates Row 30 {31 if (x = 9 & Y = 0) {32 return true; 33} 34 if (s [y] [x]) {35 if (x> 1) {36 IF (DFS (x-1, y) 37 return true; 38} 39 if (x = 1) {40 if (DFS (9, Y-1) 41 return true; 42} 43} 44 else {45 for (INT I = 1; I <= 9; I ++) {// enumerate 1 to 946 if (! Row [y] [I] &! Col [x] [I]) {// whether the current row has i47 if (Judge (Y, X, I )) {// determine whether a row can be placed in a 3 × 3 box with 48 rows [y] [I] = 1; Col [x] [I] = 1; 49 s [y] [x] = I; 50 if (x> 1) {51 if (DFS (x-1, y) {52 return true; 53} 54} 55 if (x = 1) {56 If (DFS (9, Y-1) {57 Return true; 58} 59} 60 row [y] [I] = 0; Col [x] [I] = 0; 61 s [y] [x] = 0; 62} 63} 64} 65} 66 return false; 67} 68 int main (INT argc, char * argv []) 69 {70 int N; 71 scanf ("% d", & N); 72 getchar (); 73 while (n --) {74 CLR (S, 0); 75 CLR (row, 0 ); 76 CLR (COL, 0); 77 char C; 78 for (INT I = 1; I <= 9; I ++) {79 for (Int J = 1; j <= 9; j ++) {80 scanf ("% C", & C); 81 s [I] [J] = C-'0 '; 82 row [I] [s [I] [J] = 1; // mark 83 Col [J] [s [I] [J] = 1; // mark 84} 85 getchar (); 86} 87 DFS (9, 9); // search 88 for (INT I = 1; I <= 9; I ++) {89 for (Int J = 1; j <= 9; j ++) {90 printf ("% d", s [I] [J]); 91} 92 printf ("\ n "); 93} 94} 95 return 0; 96}
View code

 

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.