I need to place N queen pieces on a MxM board so that no two queens attack each other. The difference from the original is that the queens can only attack up to 5 squares from their location, and not the whole row, column or diagonal as in the original problem.
One idea I'm playing around in my head is to place the first Queen on the board at board[i][j], but then also place it in its 5 neighboring squares as shown in the image below.
The following snipped is the isSafe function from the original N-Queens problem.
Looking for guidance as to how to check if a queen placement is safe.
def isSafe(board, row, col):
# Check this row on left side
for i in range(col):
if board[row][i] == 1:
return False
# Check upper diagonal on left side
for i,j in zip(range(row,-1,-1), range(col,-1,-1)):
if board[i][j] == 1:
return False
# Check lower diagonal on left side
for i,j in zip(range(row,N,1), range(col,-1,-1)):
if board[i][j] == 1:
return False
return True
In summary, how do I detect if a queen is safe if I limit the queen's range to 5 squares?