IMPORTANT • As work is submitted on-line, the deadline is midnight on the hand in date. • Read the marking scheme carefully. • This is an individual project and no group work is permitted. Learning...

IMPORTANT • As work is submitted on-line, the deadline is midnight on the hand in date. • Read the marking scheme carefully. • This is an individual project and no group work is permitted. Learning Outcomes • To develop problem-solving skills • To evaluate common data structures • To enhance practical programming skills. Assignment Purpose and Overview The purpose of this assignment is to implement a Sudoku (https://en.wikipedia.org/wiki/Sudoku) puzzle solver program. The puzzle can be represented as a 9×9 grid as illustrated below. 2 7 1 4 7 9 1 2 4 8 5 4 5 7 2 6 1 3 7 3 2 1 8 9 6 5 7 2 9 9 1 6 As it can be observed, some numbers are provided while some numbers are missing. The objective is to fill the missing numbers in such a way that the following constraints are met: • Row constraint: each row contains all of the numbers from 1 to 9 • Column constraint: each column contains all of the numbers from 1 to 9 • Block constraint: each of the nine 3×3 blocks, which compose the grid, contain all of the numbers from 1 to 9. A well-posed puzzle, such as the one illustrated before, has a single solution. For example, the only solution for the puzzle is illustrated below; the red numbers are the ones that should be discovered by the solver. 5 8 2 3 7 9 1 6 4 6 4 3 1 8 5 9 7 2 9 7 1 2 6 4 8 3 5 3 1 4 6 5 2 7 8 9 2 6 5 8 9 7 4 1 3 8 9 7 4 3 1 2 5 6 1 3 8 9 2 6 5 4 7 7 2 6 5 4 8 3 9 1 4 5 9 7 1 3 6 2 8 For the purposes of this assignment, in order to allow the solver to load puzzles, the puzzles will be stored in text files in the format presented below, where: • the first number indicates the number of rows of the puzzle • the second number indicates the number of columns of the puzzle • the remaining 81 numbers, presented as 9 rows of 9 columns each, represent the initially provided puzzle numbers and the missing positions, the latter represented as 0 9 9 0 0 2 0 7 0 1 0 0 0 4 0 0 0 0 0 7 0 9 0 1 2 0 4 8 0 5 0 0 4 0 5 0 7 0 0 2 6 0 0 0 0 0 1 3 0 0 7 0 3 0 2 0 0 1 0 8 9 0 6 5 0 7 0 2 0 0 0 0 0 9 0 0 0 9 0 1 0 6 0 0 The solver program must investigate if there is a solution to the puzzle, and if there is, it needs to print it to the console, accompanied by the number of steps it required to perform to reach the solution. Overall, in this assignment, you will: • Evaluate different data structures for storing Sudoku puzzles and their solutions • Implement efficient data structures for storing and manipulating Sudoku puzzles and their solutions • Design an algorithm that solves Sudoku puzzles using backtracking • Design and implement appropriate data structures for supporting the algorithm’s backtracking operation • Develop a program that implements the algorithm which solves Sudoku puzzles using backtracking Assignment Requirements and Constraints The assignment must start by defining efficient data structuresto load a Sudoku puzzle. To that end, you will define the SudokuPuzzle data structure and will use a function to initialize an instance of the data structure accordingly. In particular, the SudokuPuzzle data structure must be able to preserve the following important information about a Sudoku puzzle: • number of rows and columns in the puzzle • the Sudoku puzzle numbers in a two-dimensional array • the positions of the initially provided puzzle numbers in order to prevent programs from changing them. This must be implemented as a two-dimensional bool array named locked • the number of missing numbers in the puzzle • the start position for solving the puzzle (i.e., the position of the first 0) • the last missing element position of the puzzle (i.e., the position of the last 0) To facilitate our description, the following figure presents an example input file and what needs to be saved according to the above specification: In the above example, the number of missing numbers is 49, the first and last missing numbers are located at positions (0,0) and (8,8) respectively. As soon as the file is loaded, you should print this information on the console in the following format: +-----------------+ |0|0|2|0|7|0|1|0|0| |0|4|0|0|0|0|0|7|0| |9|0|1|2|0|4|8|0|5| |0|0|4|0|5|0|7|0|0| |2|6|0|0|0|0|0|1|3| |0|0|7|0|3|0|2|0|0| |1|0|8|9|0|6|5|0|7| |0|2|0|0|0|0|0|9|0| |0|0|9|0|1|0|6|0|0| +-----------------+ Number of missing numbers: 49 First missing number: (0,0) Last missing number: (8,8) The signature of the loading function must be the following: SudokuPuzzle* loadPuzzle(string filename) A number of puzzles are provided in Appendix 1 for you to test your implementation. Number of rows Two-dimensional array representing a Sudoku Puzzle Number of columns First empty number; the start position of the puzzle Last missing number; the finish position of the puzzle The program should then proceed to solve the puzzle. The algorithm will employ a backtracking approach according to the following high-level concept: • Test if a missing number cell can accept a valid number in the range of 1-9 • If a valid number has been found, then proceed to the next missing number • Otherwise, you have reached a dead end and you need to backtrack To facilitate our description, we provide an example based on the input file described above. The algorithm starts by setting up the missing numbers one by one reaching the following intermediate state: +-----------------+ |3|5|2|6|7|8|1|4|9| |6|4|0|0|0|0|0|7|0| |9|0|1|2|0|4|8|0|5| |0|0|4|0|5|0|7|0|0| |2|6|0|0|0|0|0|1|3| |0|0|7|0|3|0|2|0|0| |1|0|8|9|0|6|5|0|7| |0|2|0|0|0|0|0|9|0| |0|0|9|0|1|0|6|0|0| +-----------------+ The next missing number to be completed is located at position (1,2) and is highlighted with bold red font in the above figure. As it can be calculated, there is no valid number to enter for that missing position, so the program must backtrack to the previous correct state and try a different route. It is compulsory that you implement a Stack data structure to enable the backtracking functionality. The Stack data structure must be designed using generic programming techniques and should implement the following functions: • isEmpty: checks if the stack is empty • push: inserts an element to the top of the stack • pop: removes an element from the top of the stack, if it exists, without returning it • top: returns the element located on the top of the stack, if it exists As soon as the program finishes, if a solution is found, you should print this information on the console in the following format: Solution found in 1517 steps! +-----------------+ |5|8|2|3|7|9|1|6|4| |6|4|3|1|8|5|9|7|2| |9|7|1|2|6|4|8|3|5| |3|1|4|6|5|2|7|8|9| |2|6|5|8|9|7|4|1|3| |8|9|7|4|3|1|2|5|6| |1|3|8|9|2|6|5|4|7| |7|2|6|5|4|8|3|9|1| |4|5|9|7|1|3|6|2|8| +-----------------+
Jul 23, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here