Nanyang 722--Sudoku (DFS)

Source: Internet
Author: User

Sudoku time limit:MS | Memory limit:65535 KB Difficulty:4
Describe

Sudoku is a logic game that uses paper and pens to make calculations. The player needs to infer the number of all remaining spaces according to the known numbers on the 9x9 disk, and the numbers in each row, each column, and every 3*3 Palace are 1-9 and do not repeat. Every qualified Sudoku puzzle has and only one answer, and the reasoning method is based on it, and any problem without solution or multi-solution is unqualified.

One day HRDV met a claim is the world's most difficult Sudoku problem, as a qualified programmer, can casually to the difficulty bow, so he decided to make a program to solve it.

Input
The first line has a number n (0< n <100), indicating that there are n sets of test data, each set of test data is composed of a 9*9 nine Gongge, 0 means that the corresponding lattice is empty
Output
Output a 9*9 of nine Gongge, for the answer of this Sudoku
Sample input
10 0 5 3 0 0 0 0 08 0 0 0 0 0 0 2 00 7 0 0 1 0 5 0 04 0 0 0 0 5 3 0 00 1 0 0 7 0 0 0 60 0 3 2 0 0 0 8 00 6 0 90 0 4 0 0 0 0 3 00 0 0 0 0 9 7 0 0
Sample output
 
  
Source
Original
Uploaded by
tc_ Hu Jendong

The almighty violence. Reference to the Great God code written.

 //http://blog.csdn.net/bobika_/article/details/40799129#include <cstdio>#include<cstring>#include<iostream>using namespacestd;intmap[9][9], vis[9][9];intFlag;BOOLBuild (intNumintXinty) {     for(inti =0; I <9; i++)//rampant;        if(Map[i][y] = =num)return false;  for(inti =0; I <9; i++)//vertical line;        if(Map[x][i] = =num)return false; intA = x/3*3, B = y/3*3;  for(inti = A; I < A +3; i++)//3*3 Square, thought to be 9*9 in all the 3*3 are satisfied, think more (that may not exist).          for(intj = b; J < b +3; J + +)        {            if(Map[i][j] = =num)return false; }    return true;}voidDfs (intXinty) {    if(flag)return; if(x = =9&& y = =0)    {         for(inti =0; I <9; i++){             for(intj =0; J <9; J + +) {printf (J==0?"%d":"%d", Map[i][j]); } printf ("\ n"); } Flag=1; return; }    if(Y = =9)//search the next line;Dfs (x+1,0); if(Map[x][y]! =0)//skip over;Dfs (x, y +1); if(!Map[x][y]) {         for(inti =1; I <=9; i++)//A mob search;        {            if(Build (i, x, y)) {Map[x][y]=i; Dfs (x, y+1); Map[x][y]=0; }        }    }}intMain () {intT; scanf ("%d", &t);  while(t--) {flag=0;  for(inti =0; I <9; i++)             for(intj =0; J <9; J + +) scanf ("%d", &Map[i][j]); Dfs (0,0); //flag = false;    }    return 0;} 

Nanyang 722--Sudoku (DFS)

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.