POJ 2676 Sudoku

Source: Internet
Author: User

Transmission Door

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16894 Accepted: 8229 Special Judge

Description

Sudoku is a very simple task. A Square Table with 9 rows and 9 columns are divided to 9 smaller squares 3x3 as shown on the figure. In some of the cells is written decimal digits from 1 to 9. The other cells is empty. The goal is to fill the empty cells with a decimal digits from 1 to 9, one digit per cell, and in such. Each column and in each marked a 3x3 subsquare, all of the digits from 1 through 9 to appear. Write a program to solve a given sudoku-task.

Input

The input data would start with the number of the 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 are given, corresponding to the cells in this line. If a cell is empty it's 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 has the filled according to the rules. If Solutions is isn't unique, then the program could print any one of the them.

Sample Input

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127

Source

Southeastern Europe 2005 Ideas:

DFS does not have a 0 point 1-9 enumeration, which is skipped for 0

There was nothing to say, a lesson: the discriminant conditions of the cycle do not appear any calculations

15234481 njczy2010 2676 Accepted 700K 1547MS g++ 2083B 2016-03-06 09:55:21

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <stack>6#include <cctype>7#include <vector>8#include <cmath>9#include <map>Ten#include <queue> One  A #definell Long Long -  - using namespacestd; the  - intT; - intans[ -][ -]; - Chars[ -][ -]; + intFlag; -  + structPP A { at     intx; -     inty; - }; -  - intOkintXintYintv) - { in     inti,j; -i =x; to      for(j =1; J <=9; j + +){ +         if(Ans[i][j] = = v)return 0; -     } thej =y; *      for(i =1; I <=9; i++){ $         if(Ans[i][j] = = v)return 0;Panax Notoginseng     } -     intrr = (X-1)/3*3; the     intCC = (y1)/3*3; +      for(i = RR +1; I <= RR +3; i++  ){ A          for(j = cc +1; J <= CC +3; j + +  ){ the             if(Ans[i][j] = = v)return 0; +         } -     } $     return 1; $ } -  - voidDfsintXinty) the { -     inti;Wuyi     if(Flag = =1){ the         return; -     } Wu     if(x = =Ten){ -Flag =1; About         return; $     } -     intff; -     if(Ans[x][y] = =0){ -FF =0; A          for(i =1; I <=9; i++){ +             if(ok (x,y,i) = =1){ theAns[x][y] =i; -FF =1; $                 if(Y = =9){ theDFS (x+1,1); the                 } the                 Else{ theDFS (x,y+1); -                 } in                 if(Flag = =1)return; theAns[x][y] =0; the             } About         } the         if(ff = =0)return; the     } the     Else{ +         if(Y = =9){ -DFS (x+1,1); the             if(Flag = =1)return;Bayi         } the         Else{ theDFS (x,y+1); -             if(Flag = =1)return; -         } the     } the } the  the intMain () - { the     //freopen ("In.txt", "R", stdin); thescanf"%d",&T); the      for(intCcnt=1; ccnt<=t;ccnt++){94     //while (scanf ("%d%s%s", &n,a,b)!=eof) { theFlag =0; the         inti,j; the          for(i =1; I <=9; i++){98scanf"%s", S[i] +1); About         } -          for(i =1; I <=9; i++){101              for(j =1; J <=9; j + +){102ANS[I][J] = s[i][j]-'0';103             }104         } theDfs1,1);106          for(i =1; I <=9; i++){107              for(j =1; J <=9; j + +){108printf"%d", Ans[i][j]);109             } theprintf"\ n");111         } the     }113     return 0; the}

POJ 2676 Sudoku

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.