Project Euler 96 - Su Doku

Official link: https://projecteuler.net/problem=96

Thought Process

Manually solve each sudoku using backtracking algorithm

There are 3 main functions:

  1. FindEmptySlot(sudoku)

    • This function takes a sudoku as input and will find the first position which is not filled

  2. IsPositionValid(sudoku, position, number)

    • This function takes a sudoku, a specific position and a number and will check if the number if placed into the given position of the sudoku is valid, that is the number does not appear in the same box, row or column, it returns true or false

  3. SolveSudoku(sudoku)

    • This function first calls FindEmptySlot(sudoku) to find the first empty position

    • After that it will loop through 1 to 9 and call IsPositionValid(sudoku, position, number)

      1. If this returns True, we will many the assign the number to the position of the sudoku and recall the SolveSudoku(sudoku) until the entire board is filled

      2. If this returns False, we will assign 0 to the position of the sudoku, and try the next number

Interactive Code

Follow the instructions below, enter your sudoku line by line and it will output the completed sudoku

003020600
900305001
001806400
008102900
700000008
006708200
002609500
800203009
005010300