Waterloo Cup, on the recursive idea of Sudoku to solve the problem

Source: Internet
Author: User
The first time to write, is a general idea. Recently looking at some of the Waterloo Cup, the individual is a rookie level.
is to see the following link to the blogger's code after the inspiration.
Test.cpp: Defines the entry point for a console application.

#include "stdafx.h" #include <iostream> #include <math.h> #include <iomanip> using namespace std; int a[81]={0,0,5,3,0,0,0,0,0, 8,0,0,0,0,0,0,2,0, 0,7,0,0,1,0,5,0,0, 4,0,0,0,0,5,3,0,0, 0,1,0,0,7,0,


0,0,6, 0,0,3,2,0,0,0,8,0, 0,6,0,5,0,0,0,0,9, 0,0,4,0,0,0,0,3,0, 0,0,0,0,0,9,7,0,0}; Row and column have the same number, K is the value, index is the current array index bool Hang_lie (int k,int index) {int ai=index/9;//the row int aj=index%9;//with a value of the one-dimensional array for (
int i=0;i<9;i++) if (a[(AI) *9+i]==k) return true;
for (int j=0;j<9;j++) if (a[9*j+aj]==k) return true;
return false;
//Judging whether the current small square has the same number, there are 9 small squares, to ensure that each small square data only, to judge. BOOL Fangge (int k,int index) {int ai= (INDEX/9)/3; int aj= (index%9)/3; int center=ai*27+9+aj*3+1;//Each small square has a central value, This is the array index corresponding to the center value if (a[center]==k | | a[center+1]==k | | | a[center-1]==k | | a[center-9]==k | | a[center+9]==k | | a[center-9-1]==k | | A[center-9+1]==k | | A[center+9-1]==k | | A[cENTER+9+1]==K) return true;
else return false; }//Start recursive bool IsTrue (int n) {if (n>=81) return 1 while (a[n]!=0) n++;//cannot continue modifying with value, need to skip for (int j=1;j<=9;j++)///9
          Number to try, satisfied to use {if (Hang_lie (j,n) ==false && Fangge (j,n) ==false) {a[n]=j;
          if (IsTrue (n+1)) return 1;
The a[n]=0;//n+1 returns 0, indicating the need to modify N.
} return 0;
    int _tmain (int argc, _tchar* argv[]) {istrue (0);
         for (int i=0;i<81;i++) {if (i%9==0) cout<<endl;
   cout<<a[i]<< "";
return 0;

 }

Mainly worry about the computer reload system after my code is gone, some tips are not suitable for GitHub above, so the way to save the blog it haha


This is the screenshot after the run



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.