Question
In this MP, you will complete a program that solves Sudoku puzzles. If you do not know what Sudoku is, please Google it now and
In this MP, you will complete a program that solves Sudoku puzzles. If you do not know what Sudoku is, please Google it now and then continue reading this assignment.
The program uses bit-masks to represent the possible states for each square of the Sudoku puzzle. As each square can be any one of 9 numbers (1-9), we will use 9- bits (bits 0-8, where bit 0 is the least significant bit (LSB)) to represent which of the numbers each square can be. For example, if the value stored for a square is 0x14F where bits 0,1,2,3,6, and 8 are set, then this square can be the values 1,2,3,4,7, and 9 and not the values 5,6, and 8. The key to solving Sudoku puzzles is eliminating possibilities of an unknown square until there is only one remaining possibility that must be the answer. We use two rules to eliminate possibilities:
Rule 1: If we know the value for a given square (i.e., it is a singleton where there is only one possible for the value for the square), then no square in the same row, column, or 3*3 square can hold the same value. That means that we can remove that possibility from all of those squares.
Rule 2: If there is only one square in a given row, column or 3*3 square that can hold a given value, then it must be the one that holds that value. All other possibilities can be eliminated from that square. These two rules are sufficient to solve all but the most difficult Sudoku puzzles. There are two parts in this MP. In the first part, you will develop some helper methods necessary to get the code to work. In the second, you implement rule 2.
Part 1: Bit-twiddling functions (4 * 20 points) You will need to implement four functions:
bit_count: This function takes an integer and returns the count of the number of bits set( e.g., given the value 0x34 the function return the value 3). This can be done with a loop, checking whether each bit is set and incrementing a counter for each set bit.
get_nth_set_bit: This function takes an integer and an index; it iterates through the bits of the integer (from LSB to MSB) counting set bits until it comes to the indexth set bit. The current bit position is returned. For example, given the value 0x34 the function would return the following values 2,4,5 for the indices 0,1,2 respectively. The method would fail if an index of 3 or greater were provided, since only 3 bits are set in 0x34.
singleton: This function returns a bool (a type which can hold either true of false) based on whether a single bit is set. This can be done more efficiently than using the bit_count method by using the following trick: If you have an integer with only one bit set and subtract one from it, you get an integer with all bits below the bit set (but not includeing i ) set (i.e., subtracting 1 from 0x100 give 0x0ff). If you AND the original number and the new number together you will get zero. This is only true for integers with one bit set and the value zero. Please implement this method by checking two conditions for a singleton: 1) that the integer isnt zero, and 2) the AND of it and (it -1) is zero. This function should return true for 0x40 and false for 0x3.
get_singleton: Get the position of the set bit for a singleton. This is much like get_nth_set_bit.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started