A brief summary of the main ideas of JavaScript traversal solving sudoku problem _javascript skills

Source: Internet
Author: User

Sudoku Rule
Sudoku Game, the classic for 9x9=81 cells composed of nine Sudoku, but also formed a 3x3=9 small nine Sudoku, required in 81 small cells fill in the number of 1~9, and the number in each row and each small nine grid can not repeat.

Sudoku Skills

    • Intuitive method
    • Candidate Number method
    • Correlation 20: A number is only related to the row and column and the small nine sudoku of the 20 lattice.

My train of thought

    • The validity judgment function is carefully designed, and the validity of the scheme can be determined by traversing 81 small cells at a time.
    • In the same time, the relevant 20 lattice decision is designed, and a 0~9 cycle is completed to determine the validity.
    • Simulates the stack with an array to provide backtracking information for the search.
    • The object has a map property to help judge the validity of the scheme, and greatly simplifies the algorithm.

Scheme Design and implementation
only a two-dimensional array storage Sudoku scheme, a one-dimensional array stack, a Boolean variable for backtracking identification.

1. Variable definition:

var problem = [        //This is the title of difficulty 10.7 mentioned in the book
  [8,0,0,0,0,0,0,0,0],
  [0,0,3,6,0,0,0,0,0], [
  0,7,0,0,9,0,2,0,0] , [
  0,5,0,0,0,7,0,0,0], [
  0,0,0,0,4,5,7,0,0],
  [0,0,0,1,0,0,0,3,0],
  [0,0,1,0,0,0,0,6,8],
  [0,0,8,5,0,0,0,1,0],
  [0,9,0,0,0,0,4,0,0]
]
var stack = [],flag = false;

2. Validation of the scheme:
make full use of the Hacht of JavaScript objects, in order to facilitate debugging, to determine the effective time return value of the function of 0, invalid hours three cases, row conflict, row conflict, small nine Sudoku conflict, respectively, return 1, 2, 3. Early judgment used it, and then added the relevant 20 lattice decision, in search of the answer when the function is not used.

function Checkvalid (sudo) {let Subsudo = {}//auxiliary variable, used to determine whether the small nine Sudoku conflict for (Let i = 0; i<9; i++) {Let row =      {}, col = {}//auxiliary variable, used to determine whether row, column conflicts for (Let j = 0; j<9; j +) {Let Cur1 = sudo[i][j], CUR2 = Sudo[j][i]          An internal cycle to complete the ranks of the decision if (ROW[CUR1])//The current element has been in the row, the optimization of the judgment of 0, the key is 0 time value of 0, do not need additional judgment return 1; Return error code else ROW[CUR1] = CUR1//current element not present in row, in auxiliary variable if (COL[CUR2])//column's decision is similar to row, optimize the judgment of 0, K
      EY is 0 with a value of 0 and does not require an extra judgment return 2;
      else COL[CUR2] = CUR2; Let key = Math.floor (I/3) + '-' +math.floor (J/3)//For different small nine Sudoku to generate different key if (Subsudo[key)) {//small nine lattice already have elements, optimize off 0 of the judgment, K EY is 0 with a value of 0, no additional judgment is required (SUBSUDO[KEY][CUR1])//The determination of a small nine sudoku is similar to a line return 3 else Subsudo[key
        ][CUR1] = Cur1}else{//This is the first element in a small nine cell subsudo[key] = {}//For small nine Sudoku create a new auxiliary variable and deposit the first element in it       SUBSUDO[KEY][CUR1] = cur1}} return 0;         Programs can run to this, stating that the scheme is valid}

 
3. Related 20 lattice decision
Principle with the whole judgment, bright spot in small nine Sudoku positioning.
function Check20grid (sudo,i,j) {let        
  row = {}, col = {}, Subsudo = {}        //auxiliary variable for
  (let k = 0; k < 9; k++) {
   
    let cur1 = Sudo[i][k], CUR2 = Sudo[k][j]
    if (cur1) {                    //The current element is already in the row, optimizing the judgment of 0, the key is 0 value 0, no additional judgment is required
      (ROW[CUR1) ) return
        1;                Return error code
      else
        Row[cur1] = cur1            //Current element not present in row, save in auxiliary variable
    }
    if (CUR2) {                    //column's decision is similar to row, optimize out 0 judgment , the key is 0 with a value of 0, and no additional judgment is required
      if (COL[CUR2]) return
        2;
      else
        col[cur2] = cur2;
    }
    Transformation cycle variable to small nine sudoku coordinates let
    key = Sudo[math.floor (I/3) *3 + math.floor (K/3)][math.floor (J/3) *3+math.floor (k%3)]
    if (Subsudo[key])                //Nine decision is similar to row, optimize the judgment of 0, key is 0 value 0, no additional judgment return
      3
    else
      subsudo[key] = key
  return
  0;
}

   

4. Traversal Solver
an element with an element state initial value of zero is a pending feature, with the aid of a stack and no additional storage space.

 function Findanswer () {for (Let i = 0; i<9; i++) {for (Let j = 0; j<9;)
        {if (problem[i][j] = = 0 | | | flag) {//The current position is the first processing of the pending element or backtracking to the current position, the two situations seem different, in fact the processing is the same, since add 1 can flag = false;        Let k = problem[i][j] + 1;          Search down a legal value step while (K<10) {//loop to find the next legal value problem[i][j] = k;
            Fill in if (Check20grid (problem,i,j) = = 0) {//Legal, relevant 20-Judge Stack.push ([i,j++])//store backtracking points, and step into
          Break
        } k++;          } if (k>9) {//Current position cannot find legal value, backtracking problem[i][j] = 0;         Backward back to zero let RT = Stack.pop ();  
          The stack takes backtracking information if (!RT)//No judgment, returns 0 return 0;
        I=RT[0]//cross J=RT[1] flag = true;
      }}else{//Current position number for topic given J + +;                      }} return 1; Successfully found a set of solutions} 

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.