# The implementation method of C + + for programming beauty Sudoku Solver

Source: Internet
Author: User

Programming the beauty of the first chapter of the 15th section, talking about the construction of Sudoku, a start to get this problem really no idea, but read the book in the introduction, found that the original solution of the idea and the N queen problem is consistent, but do not know why, anyway, at first did not think of this backtracking method, know that is solved by backtracking, The problem becomes much easier.
Here we do not intend to implement the construction of Sudoku, instead, we implement a Sudoku Solver, and MOM will no longer have to worry about my sudoku.

Of course, the idea of solver and construction of the idea of Sudoku, are backtracking search, there is no longer too much to explain.

Program Run Instructions:
1. Place the Sudoku data to be solved in the In.txt file, and the program will automatically read it and output the solution to the screen and the OUT.txt file.

2. Console color change using the Setconsoletextattribute function, use details can refer to C/C + + to change the console output font background and color (Windows)

3. For program run time statistics, use GetTickCount () or clock () can be

The results are as follows:

The red indicates that the part is the solved value >_<~~

By convention, the following is the source code:

``//================ "Shudu.h" ===========#pragma once#Define N 9#include <fstream>typedefstruct_searchnode {intRowintColintdata;} Searchnode;class cshudu{ Public:Cshudu(); ~cshudu ();voidCreatesolution ();Private:BOOL IsOK(intRowintCOL);voidSolveintSEARCHNODE_ID);voidWriteFile (intID);voidDisplayintID);voidInitshudu ();voidOutputintID);Private:intM_shudu[n + 2][n + 1];BOOLM_find;intm_solution_id;    Std::fstream outfile; Searchnode m_searchnode[n * N];intM_searchlength;};``
``//===================== "Shudu.cpp" ==================#include <windows.h>#include "ShuDu.h"#include <stdio.h>#include <string.h>#include <fstream>#include <iostream>Cshudu::cshudu () {memset(M_shudu,0,sizeof(M_shudu)); M_find =false; m_solution_id =0; Outfile.open ("OUT.txt",STD:: Ios::out); M_searchlength =0;memset(M_searchnode,0,sizeof(M_searchnode));} Cshudu::~cshudu () {outfile.close ();}voidCshudu::createsolution () {Initshudu (); Solve1);}BOOLCshudu::isok (intRowintCol) {BOOLIsOK =true;//Column Direction detection     for(inti =1; I! =Ten; i++) {if(i = = row)Continue; IsOK = (M_shudu[i][col] = = M_shudu[row][col])?false:true;if(IsOK = =false)returnIsOK; }//Line detection     for(inti =1; I! =Ten; i++) {if(i = = col)Continue; IsOK = (M_shudu[row][i] = = M_shudu[row][col])?false:true;if(IsOK = =false)returnIsOK; }//Zone detection    intBlock_start_row = (Row-1) /3*3+1;intBlock_start_col = (Col-1) /3*3+1; for(inti = Block_start_row; I! = Block_start_row +3; i++) { for(intj = Block_start_col; J! = Block_start_col +3; J + +) {if(i = = Row && j = = col)Continue; IsOK = (M_shudu[i][j] = = M_shudu[row][col])?false:true;if(IsOK = =false)returnIsOK; }    }returnIsOK;}voidCshudu::solve (intSEARCHNODE_ID) {if(M_find = =true)return;if(M_searchlength +1= = searchnode_id) {//m_find = true;m_solution_id++; Output (m_solution_id);return; } for(inti =1; I! =Ten; i++) {introw = M_searchnode[searchnode_id].row;intcol = M_searchnode[searchnode_id].col; M_shudu[row][col] = i;if(IsOK (Row, col)) {Solve (searchnode_id +1); row = m_searchnode[searchnode_id +1].row; Col = m_searchnode[searchnode_id +1].col; M_shudu[row][col] =0; }               }}voidCshudu::d isplay (intID) {STD::cout<<"=========================== First"<< ID <<"Group Sudoku data ============================="<<STD:: Endl; System"title Sudoku Solver by zhyh2010 version 1.0");intsearch_id =1; HANDLE HANDLE = GetStdHandle (Std_output_handle); for(inti =1; I! =Ten; i++) { for(intj =1; J! =Ten; J + +) {if(i = = M_searchnode[search_id].row && j = = M_searchnode[search_id].col) {Setconsoletextattribute (handle, Foreground_intensity |                foreground_red);            search_id++; }ElseSetconsoletextattribute (Handle, Foreground_intensity | foreground_red | Foreground_blue | Foreground_green);STD::cout<< M_shudu[i][j] <<" "; }STD::cout<<STD:: Endl; }STD::cout<<STD:: Endl;STD::cout<<STD:: Endl; Setconsoletextattribute (handle, foreground_intensity);}voidCshudu::writefile (intID) {outfile <<"=========================== First"<< ID <<"Group Sudoku data ============================="<<STD:: Endl; for(inti =1; I! =Ten; i++) { for(intj =1; J! =Ten; J + +) {outfile << m_shudu[i][j] <<" "; } outfile <<STD:: Endl; } outfile <<STD:: Endl; OutFile <<STD:: Endl;}voidCshudu::output (intID) {display (ID); WriteFile (ID);//getchar ();}voidCshudu::initshudu () {STD:: Ifstream infile ("In.txt"); for(inti =1; I! =Ten; i++) { for(intj =1; J! =Ten; J + +) {infile >> m_shudu[i][j];if(M_shudu[i][j] = =0) {m_searchlength++;                M_searchnode[m_searchlength].row = i;            M_searchnode[m_searchlength].col = j; }        }    }}``
``//===================== "Sudoku Solver main.cpp" ==================//@ author:zhyh2010//@ date:20150624//@ version:1.0//@ description:c++//===================== "Sudoku Solver" ==================#include <stdio.h>#include "ShuDu.h"#include <time.h>voidMain () {//dword T_start = GetTickCount ();    Autot = Clock ();    Cshudu instance; Instance. Createsolution ();//dword t_end = GetTickCount ();    printf("program run time is%d ms\n", +*static_cast<float> (Clock ()-T)/clocks_per_sec);}``

The implementation method of C + + for programming beauty Sudoku Solver

Related Keywords:

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.