In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of December 14th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #09 exercise sheet]


In this problem you should submit a function as described. Inside the function do not print anything that was not asked!

(in this class you are expected to write a solution that uses recursion - mooshak will not force it, but that should be your goal)

[IP092] Chess Puzzle

Dr. Zoidberg has been given an intriguing puzzle to solve. He's been tasked with placing exactly C queens on a chessboard with R rows and C columns. He needs to know all the possible ways to place these queens on the board, such that no two queens can attack each other.

As a reminder, queens in chess can move any number of squares vertically, horizontally, or diagonally. Therefore, Zoidberg must ensure that no two queens are placed in the same row, the same column, or the same diagonal.

Zoidberg needs your help to determine all the valid arrangements of placing the queens on the chessboard.

The Problem

Write a function queens(rows, cols) that generates all possible valid arragements of cols queens on a chess board of rows by columns such no queen can attack each other. The function should return a list of all arragenments. Each arragement should be given as a list of integers indicating the rows of queen, with the position in the list indicating the column. Rows start at 1.

For instance, the following valid arragement on a 8 by 8 board would be represented by [1,7,4,6,8,2,5,3].

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

1 ≤ cols ≤ rows ≤ 10       number of rows and columns

Example Function Calls Example Output
output = queens(4, 4)
print(sorted(output))

print()

output = queens(6, 5)
print("output is of type", type(output))
print("output has size:", len(output))
for x in sorted(output): print(x)
[[2, 4, 1, 3], [3, 1, 4, 2]]

output is of type 
output has size: 40
[1, 3, 5, 2, 4]
[1, 4, 2, 5, 3]
[1, 5, 2, 6, 3]
[2, 4, 1, 3, 5]
[2, 4, 6, 1, 3]
[2, 4, 6, 1, 5]
[2, 4, 6, 3, 5]
[2, 5, 1, 6, 4]
[2, 5, 3, 1, 4]
[2, 5, 3, 6, 4]
[2, 6, 1, 3, 5]
[2, 6, 3, 1, 4]
[3, 1, 4, 2, 5]
[3, 1, 6, 2, 5]
[3, 1, 6, 4, 2]
[3, 5, 2, 4, 1]
[3, 5, 2, 4, 6]
[3, 6, 2, 5, 1]
[3, 6, 4, 1, 5]
[3, 6, 4, 2, 5]
[4, 1, 3, 5, 2]
[4, 1, 3, 6, 2]
[4, 1, 5, 2, 6]
[4, 2, 5, 3, 1]
[4, 2, 5, 3, 6]
[4, 6, 1, 3, 5]
[4, 6, 1, 5, 2]
[4, 6, 3, 5, 2]
[5, 1, 4, 6, 3]
[5, 1, 6, 4, 2]
[5, 2, 4, 1, 3]
[5, 2, 4, 6, 3]
[5, 2, 6, 1, 3]
[5, 3, 1, 4, 2]
[5, 3, 1, 6, 2]
[5, 3, 1, 6, 4]
[5, 3, 6, 4, 2]
[6, 2, 5, 1, 4]
[6, 3, 5, 2, 4]
[6, 4, 2, 5, 3]

Introduction to Programming (CC1024)
DCC/FCUP - University of Porto