Question: solve ( ) This function should accept three parameters named puzzle, row, and col. The parameter puzzle is expected to be a list representing a

solve()
This function should accept three parameters named puzzle, row, and col. The parameter puzzle is expected to be a list representing a puzzle state, while row and col are expected to be integers indicating the position of a specific cell in the puzzle. The default values of row and col should be set to 0, indicating that the function will begin the serach for a solution by looking at the upper-left cell.
The function should return a list of lists representing the solved state of the puzzle, if one exists. If no solution exists, then the function should return None.
The function should find the solution to the puzzle recursively by following the steps below:
Check to see if the cell indicated by the parameters row and col is blank (which would be indicated by a value of 0 stored at the appropriate location in puzzle). If this cell is not blank, then we will move to the next cell, if one exists. In this case, perform the following steps:
a. Use get_next () to obtain the position of the next cell. Store the resulting values in next_row and next_col.
b. Check to see if next_row is None. If so, then we are at the last cell, and will have finished construction the solution. Return puzzle, which will store the completed solution.
c. If next_row is not None, then we will move on to the next cell. Call solve() again, passing it puzzle, next_row, and next_col.
If the current cell is blank, then we will identify the possible values that could be placed within this cell. Call get_options(), passing it puzzle and the location of the current cell. Store the results in a list named options.
If options is empty, then there are no value digits that could be placed in this cell. Return None to indicate that the current puzzle state is not solvable. This will end this branch of the recursion, causing the function to backtrack and consider different values for previously filled-in cells.
If options is not empty, then loop over the digits in this list. Perform the following steps for each such value:
a. Use copy_puzzle() to create a new copy of the puzzle, named new_puzzle.
b. Use row and col to set the entry of new_puzzle corresponding to the current cell to the current value of options that is being considered. We are filling in a valid number in this cell and will check to see if this leads to a solution.
c. Call solve(), passing it new_puzzle, row, and col. Store the result in a variable named result. This is the step in which we attempt to see if the new puzzle state leads to a solution. Note that if a solution was found, then result will contain that solution. If no possible solution can be found using the current state, then result will contain None.
d. If result is not equal to None, then this means that a solution was eventually found. Return result, which should store the solved puzzle state.
e. If result is equal to None, then no additional action should be taken. In this case, the loop will continue executing, and the function will try out different values for the current cell.
solve ( ) This function should accept three

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!