Question: Task: In Python, implement the Nqueens.is _ valid method to check for conflicting queens trying to solve the N - queens problem ( e .
Task: In Python, implement the Nqueens.isvalid method to check for conflicting queens trying to solve the Nqueens problem eg are any queens in conflicting columns or diagonals with another queen In this example, n refers to the number of rows and columns on the board and the number of queens. I am running into some logic issues with my code for this method. Below are the task parameters:
The starter code provided already implements both the bruteforce algorithm using the improved representation and the backtracking algorithm. However, both algorithms rely on the Nqueens.isvalid method to determine if a given placement of queens is valid up to the specified number of rows.
You will need to implement the Nqueens.isvalid method to check that the positions stored in the queens array do not conflict on any column or diagonal. You do not need to check for conflict rows because the implementation prevents two queens from being assigned to the same row ie it uses an array where the index represents the row of the queen
To check that there is not a column conflict you will need to check that for all queens from row up to but not including row "rows" no two queens have the same column value.
To check that there is not a diagonal conflict, you will need to check that for all queens from row up to but not including row "rows" no two queens share a diagonal. You can implement this a variety of ways but one approach is to check one diagonal by adding the row and column together for each queen. If any two queens have the same value, they are on the same diagonal. You also need to check the other diagonal by subtracting the column from the row. If any two queens have the same value, they are on the same diagonal. You will prove this is true later on in the assignment.
The starter code provided so far:
class Nqueens:
def initself n:
self.n n
self.queens n
def printsolutionself:
Print the board using the positioning found in the
queens array. If queens you should print No solution"
When printing the board use to separate columns and
new lines to separate rows. Mark queens with a Q and empty
squares with a
pass
def isvalidself rows:
Check if the positioning listed in the queens arrays is valid up to "rows"
Ignore queens with an index rows.
A valid position is one which does not share a column or diagonal with
any other queen."""
# Check columns and diagonals for conflicting queens
# Columns: Check that all queens from row up to but not including the current row do not have any queens with the same index value
# Diagonals: Check that all queens from row up to but not including the current row do not have any queens that share a diagonal
# To do so: Check for a diagonal in one direction by adding the row and column together for each queen
# To check for the other diagonal, subtract the column from the row
# In either direction, if any two queens have the same resulting value, they are on the same diagonal
pass
def backtrackself row:
if row self.n:
return self.isvalidrow
for col in rangeselfn:
self.queensrow col
if self.isvalidrow and self.backtrackrow :
return True
self.queensrow
return False
def backtrackingfindsolutionself:
self.backtrack
def bruteforceself row:
if row self.n:
return self.isvalidrow
for col in rangeselfn:
self.queensrow col
if self.bruteforcerow :
return True
self.queensrow
return False
def bruteforcefindsolutionself:
self.bruteforce
My current attempt at implementing isvalid:
for i in range rows:
for j in range rows:
if selfqueensi self.queensj or absselfqueensi i absselfqueensj j
or absi self.queensi absj self.queensj:
return False
else:
return True
When running unit tests, only testisValidTrueself ie checking for isvalid returning true, is passing. Could someone please take a look at my logic for isvalid Thank you!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
