Presentation Number 12 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Presentation Number 12

Description:

In the final project you are required to write a Sudoku solver. Sudoku. Rules: ... Sudoku. Each cell is part of a region, row and column. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 37
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Presentation Number 12


1
Presentation Number 12
  • C Programming Course

2
The Final Project
  • In the final project you are required to write a
    Sudoku solver.

3
Sudoku
  • Rules
  • The puzzle is defined over a 9x9 grid made up of
    3x3 subgrids (called regions).
  • The aim of the puzzle is to fill in the missing
    cells with the numbers 1 through 9.
  • Each row, column and region must contain only one
    instance of each numeral.

4
Sudoku
  • Each cell is part of a region, row and column.
  • When a cell is assigned a number, it effects the
    assignments of its region, row and column.

Assign a number to this cell
These cells are effected
5
Possible Assignments
  • The region eliminates the numbers
  • The row eliminates the numbers
  • The column eliminates the numbers

6
Possible Assignments
  • This leaves the numbers
  • as possible assignments.

7
These are the options that are left for each cell
after we update the board with the initial numbers
8
These are the options that are left for each cell
after we update the board with the initial numbers
Notice that the red cells were automatically
solved
9
Updating the board according to these cells
solves more cells
10
And again ...
11
And again ...
12
And again ...
13
And again ...
14
And again ...
15
And again ...
16
And again ...
17
And again ...
18
And again ...
19
And again ...
20
And again ...
21
And again ...
The puzzle is solved
22
Updating the Board
  • By repeatedly updating the board, setting numbers
    and eliminating options the puzzle (or at least
    most of the puzzle) can be solved.

23
Updating the Board
  • If a cell has only one option and we havent
    updated the board according to this cell
  • We update the board and mark the cell
  • (so we wont repeat the update again).
  • We repeat this until we can no longer update the
    board.

24
Cell Data Structure
  • A cell is a struct that stores
  • A flag for each one of the nine options.
  • A flag that says if we used this cell to update
    the board.

Options Update Flag
Yes / No
25
The Options That Remain
  • Many of the puzzles cannot be solved by simply
    updating the board.
  • Usually, we will have some cells that have more
    than one option.
  • What do we do with them?

This cell can be 2, 4 or 9
26
The Options That Remain
  • Copy the board
  • Find an unsolved cell
  • Remove the first option for the cell in the
    original board.
  • Remove all the options except the first in the
    copy of the board.

27
The Options That Remain
  • We will use recursion to solve the puzzle.
  • If we can solve the puzzle using the new copy of
    the board then we found our solution.
  • Otherwise, the original board is closer to the
    solution than before.

28
Pseudo-Code
  • flag SolveSudoku( board )
  • UpdateBoard( board )
  • solutionFlag CheckBoard( board )
  • if ( solutionFlag SOLVED )
  • PrintBoard( board )
  • return SOLVED
  • if ( solutionFlag NO_SOLUTION )
  • return NO_SOLUTION

29
Pseudo-Code
  • CopyBoard( board, copy_board )
  • id FindUnsolvedCell( board )
  • SetOption( board, copy_board, id )
  • solutionFlag SolveSudoku( copy_board )
  • if ( solutionFlag SOLVED )
  • return SOLVED
  • else
  • return SolveSudoku( board )

30
Pseudo-Code
  • Solve the board. If there is a solution print
    it, otherwise report that there is no solution.
  • flag SolveSudoku( board )
  • Update the board according to cells that have
    only one solution and were not used to update the
    board before.
  • UpdateBoard( board )
  • Checks if a board is solved, has no solution or
    has not been solved yet.
  • CheckBoard( board )
  • Prints the board.
  • PrintBoard( board )

31
Pseudo-Code
  • Copies a board into another board.
  • CopyBoard( board, copy_board )
  • Finds a cell that has more than one option left
    and returns its id.
  • id FindUnsolvedCell( board )
  • The function updates a cell (given by an id
    number) in both boards. In the original board it
    removes the first option. In the copy_board it
    removes everything except the first option.
  • SetOption( board, copy_board, id )

32
Input / Output
  • Input Output
  • 5 3 0 0 7 0 0 0 0 5 3 4 6 7 8 9 1 2
  • 6 0 0 1 9 5 0 0 0 6 7 2 1 9 5 3 4 8
  • 0 9 8 0 0 0 0 6 0 1 9 8 3 4 2 5 6 7
  • - - - - - - - - - - - - - - - - - -
  • 8 0 0 0 0 0 0 0 3 8 5 9 7 6 1 4 2 3
  • 4 0 0 8 0 3 0 0 1 4 2 6 8 5 3 7 9 1
  • 7 0 0 0 2 0 0 0 6 7 1 3 9 2 4 8 5 6
  • - - - - - - - - - - - - - - - - - -
  • 0 6 0 0 0 0 2 8 0 9 6 1 5 3 7 2 8 4
  • 0 0 0 4 1 9 0 0 5 2 8 7 4 1 9 6 3 5
  • 0 0 0 0 8 0 0 7 9 3 4 5 2 8 6 1 7 9

33
Input / Output
  • The number 0 represents an empty cell.
  • The signs , - and are region separators.
  • All characters are separated by spaces.
  • If the input board has no solution print
  • This board has not solution!.
  • If an error has occurred during the run of the
    program it should return -1 and print Error.
  • You can assume that the initial input is correct
    (and legal).

34
Solution
  • The solution presented here is only a suggestion.
  • Feel free to change whatever you like.

35
Submission Date?
36
Questions?
  • about anything that was learned during the course
Write a Comment
User Comments (0)
About PowerShow.com