Question: Please answer using python language thanks. Task 1a: n-Queens partial Useful material: In this task you may find it useful to reuse some of your
Please answer using python language thanks.




Task 1a: n-Queens partial Useful material: In this task you may find it useful to reuse some of your code from Workshop 6. Write a program getPossible(partial, n) in Python that takes as input a list partial representing a partial solution to the n-Queens problem and a positive integer n representing the dimension of the nxn chessboard. The input list contains the positions of the Queens currently placed on the board (or is empty if no Queens have been placed). You should use the same list representation as you used in Task 3 of Workshop 6, that is, the entry partial[i] gives the row position of the Queen in column i. You may assume that the partial solution gives the positions of the Queens in the first i columns and that the other columns are empty. Your program should return a list containing possible row positions for a Queen to be placed in the next column on the board. (The index of the next column can be obtained by the length of partial.) For example: If your partial list is [5, 3] and n = 6, your program should return the list [0, 1], that is, a Queen could be placed at row 0 column 2, or row 1 column 2, of the board. If your partial list is empty and n = 5, your program should return the list [0, 1, 2, 3, 4]. If your list is [3, 1] and n = 4, your program should return an empty list. Task 1b: In order to test your program in Task 1a, write a function printTable(partial, board_size) so that it prints a table representing the chessboard. It should have q in entries where a queen is placed, x in entries where a Queen may be placed in the next column and 0 in all other entries. For example: If your list is [5, 3] and n = 6, your program should print: 00000 00000 000000 00000 000000 900000 If your program getPossible is correctly implemented, a Queen placed on a position marked 'X' cannot "attack any of the positions marked 'Q'. So you can check if 'X' marks a valid position. What else should you check for? Task 2a: n-Queens full implementation Useful material: In this task you may find Lecture 17 "Implementing Backtracking useful. The following is the backtracking algorithm that was discussed in the lectures (lecture 17 slide 16). It takes as input a list partial and a positive integer board_size, the dimension of the nx n chessboard, and prints all solutions to the n-Queens problem using backtracking. []: def queens(partial, board_size): possibleItems = getPossible(partial, board_size) print(partial, II possibleItems) if possibleItems updateSolution (partial, board_size) else: for item in possibleItems: partial.append(item) queens (partial, board_size) partial.pop() return True Using the code above, and the function getPossible from Taskla, implement the backtracking algorithm for n-Queens. You will need to write the function for update Solution. This function will test when the partial solution is a solution. Call your function using queens([],board_size) Enter board size: 4 [1, 3, 0, 2] [2,0, 3, 1] Enter board size: 6 [1, 3, 5, 0, 2,4] [2,5, 1, 4,0, 3] [3, 0, 4, 1, 5, 2] [4, 2, 0,5, 3, 1] Task 2b Modify your updateSolution code to instead print the table of the output and to count the number of solutions. For example: Enter board size: 6 000 000 Q00000 000000 0Q0000 000000 00Q000 000000 00Q000 Q00000 000000 000000 O Q0000 000000 000 000 00000 Q00000 00Q000 000000 00Q000 000000 O Q0000 000000 Q00000 000 000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
