Realization of C + + by Sudoku Solution

Source: Internet
Author: User

Transfer from http://blog.csdn.net/qq_31558353/article/details/50615760

Use a demo column (said to be the world's hardest Sudoku, 10s over)

//<pre code_snippet_id= "1592833" snippet_file_name= "blog_20160301_1_4048211" name= "code" class= "CPP" ># Include <iostream>#include <iostream>using namespacestd;/*Construct Completion Flag*/BOOLSign =false; /*Create a sudoku matrix*/intnum[9][9]; /*function Declaration*/voidInput ();voidOutput ();BOOLCheck (intNintkey);intDFS (intN); /*Main function*/intMain () {cout<<"Please enter a 9*9 matrix, the vacancy is indicated by 0:"<<Endl;    Input (); DFS (0);    Output (); System ("Pause");} /*read-in Sudoku matrix*/voidInput () {Chartemp[9][9];  for(inti =0; I <9; i++)    {         for(intj =0; J <9; J + +) {cin>>Temp[i][j]; NUM[I][J]= Temp[i][j]-'0'; }    }} /*Output Sudoku Matrix*/voidOutput () {cout<<Endl;  for(inti =0; I <9; i++)    {         for(intj =0; J <9; J + +) {cout<< Num[i][j] <<" "; if(J%3==2) {cout<<"   "; }} cout<<Endl; ifI3==2) {cout<<Endl; }    }} /*determine if key fills in N to satisfy the condition*/BOOLCheck (intNintkey) {    /*determine if n is Heng lie legal*/     for(inti =0; I <9; i++)    {        /*j for N Vertical coordinates*/        intj = N/9; if(Num[j][i] = = key)return false; }     /*determine if n is valid for the vertical column*/     for(inti =0; I <9; i++)    {        /*j is n horizontal axis*/        intj = n%9; if(Num[i][j] = = key)return false; }     /*x is n the small nine Gongge left vertex vertical coordinate*/    intx = N/9/3*3; /*y is n the small nine Gongge left vertex horizontal axis*/    inty = n%9/3*3; /*determine if n is the small nine Gongge legal*/     for(inti = x; I < x +3; i++)    {         for(intj = y; J < Y +3; J + +)        {            if(Num[i][j] = = key)return false; }    }     /*all legal, return right*/    return true;} /*Deep Search Construction Sudoku*/intDFS (intN) {    /*all of them are compliant, exit recursion*/    if(N > the) { sign=true; return 0; }    /*Skip when the current bit is not empty*/    if(num[n/9][n%9] !=0) {DFS (n+1); }    Else    {        /*Otherwise, the current bit is enumerated for testing*/         for(inti =1; I <=9; i++)        {            /*fill in numbers when conditions are met*/            if(Check (n, i) = =true) {Num[n/9][n%9] =i; /*Continue search*/DFS (n+1); /*returns if the construct succeeds, exits directly*/                if(Sign = =true)return 0; /*if the construct is unsuccessful, restore the current bit*/Num[n/9][n%9] =0; }        }    }}

Input

Feel the output

A computer is a great invention.

Baidu Encyclopedia on the world's most difficult Sudoku, garbage ~ ~

Realization of C + + by Sudoku Solution

Related Article

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.