![]() No more moves or other error, return NULL to indicate dead endįree(pBoard) // This board is dead - clean up memory PDoneBoard = SolveBoard(pNewBoard, ++nLevel) PNewCell = (pFirstMove - pBoard + pNewBoard) // pointer maths For each available move in the first available cell PTCELL pFirstMove = FindFirstMove(pBoard) // pointer to cell with first available move This is a BFI approach (exhaustive search) If there are more moves, find the first one and test each possibility If the board is completed, return the completed board to the caller Now check the board status and act accordingly and keep doing this until we have not changed any cells First use deductive logic to place all the possible values on the board PTCELL SolveBoard(PTCELL pBoard, int nLevel) If ((EMPTY_CELL = pCell->usValue) & (EMPTY_MASK != pCell->usMask)) Find the first cell that has a valid move in it.įor (i=0, pCell=pBoard iusValue, pCell->usMask) Return the board status to decide if there are any valid moves left or we #define _BOARD_SIZE (10*MAX_ELEMENT*MAX_ELEMENT*sizeof(TCELL)) Create a new puzzle board and, if pBoard not null, copy an existing board PCell->usValue = (unsigned short)Mask2Value(pCell->usMask) printf("New Value : %d\n", i, j, Mask2Value(pCell->usMask)) If (CountMask(pCell->usMask) = 1) // we found the value! printf("New Mask : 0x%x (%d)\n", i, j, pCell->usMask, CountMask(pCell->usMask)) PCell->usMask = CreateNewMask(pBoard, i, j) // check possibles function when there were no changes made in the previous pass.įor (i=0, pCell=pBoard iusValue) // not set yet function) how iften it should be called. The number of changes is counted so that we can control (away from this If there is just one number, then change the cell to that. For each cell on the board, check what possible values can be put in that For the value to be unique in the column, check all rows at thatįor (i = 0, pCell = pBoard + (nCol*sizeof(TCELL)) Counts the bits turned on and returns that count. Count how many valid values are in each mask value. printf("Read : %d\n", i, j, pCell->usValue) PCell->usMask = ((k=0) ? usInitMask : EMPTY_MASK) initialise first mask - each cell could can be value Where a value is missing, the EMPTY_CELL value (0) should be performed and it is assumed that MAX_ELEMENT*MAX_ELEMENT values will be Text is organised as numbers separated by white space. Read the initial board position from standard input, although it can be from Printf(": %s\n", i, j, MaskValueString(cBuf, pCell->usMask)) ![]() print them out after the board (ie, here). If we need to show the possible values for incomplete cells, then Printf("\nFIRST SOLUTION\n=\n\n", nPass) įor (i=0, pCell=pBoard iusValue != EMPTY_CELL) #define SetMask(i) ((unsigned short)(1 0) Programmatically (in C), the resulting control code can be found in the SolveBoard function in the code C console style application below. Recursively test this new configuration (ie, apply these rules to a copy of the new configuration).Guess a solution for an unknown square.Apply the rules until they cannot generate any more solutions.This essentially defines a recursive software algorithm to exhaustively search for a solution. ![]() If the ‘guess’ is incorrect, then recursive versions of the board will result in a violation to the rules, no possible solution and the need to try a different value for the unsolved square.If the ‘guess’ was correct, the recursive versions of the board will result in a final solution.At this point the puzzle is either solved, in which case there are no more possible moves, or a possible solution is tried for one of the unsolved squares. At any time during the solving sequence, the rules are repeatedly applied until there are no more moves. To solve a puzzle, we need to consider what needs to be done for any configuration of a board. Every properly constructed puzzle has just one correct solution. The puzzle setter provides a partially completed grid for the puzzler to solve. Each nine box sub-grid must have the numbers 1-9 occurring just once.Each column must have the numbers 1-9 occurring just once.Each row must have the numbers 1-9 occurring just once.Their simplicity should make a software solver possible, so I thought I would give it a try. The simple rules of this game define the algorithm for solving the puzzle. The classic Sudoku game is a number puzzle involving a grid of 81 squares, divided into into nine blocks, each containing nine squares.įrench newspapers featured variations of the puzzles in the 19th century, but modern sudoku only started to become popularised in 1986 by the Japanese puzzle company Nikoli, under the name Sudoku, a contraction of the full name of the puzzle meaning single number in Japanese. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |