-->
当前位置:首页 > 题库

PROGRAMMING:Eight queens with mines on chessboard

Luz5年前 (2021-05-10)题库406
In chess, the queen is the most powerful piece. She can walk horizontally, straightly and obliquely. In 1848, chess player Max Bessel put forward the famous eight queens problem, that is, in August × Eight queens are placed on the chessboard of 8, so that they cannot attack each other - that is, no two Queens can be in the same row, column or diagonal line.
**There are mines in a certain grid of chessboard that can't be put on the queen. How many kinds of pendulum methods are there**
###Input format:
The coordinates x and y of the mine grid are separated by spaces
###Output format:
Number of placement methods
###Input example:
Here is a set of inputs. For example, ` 2,4 'means the second row and the fourth column.
```in
2 4
```
###Output example:
The corresponding output is given here. For example:
```out
eighty-four
```







answer:If there is no answer, please comment
```
#Board is a list to store the current status of the queen
def conflict(board,nextx):
nexty=len(board)
for i in range(nexty):
if abs(board[i]-nextx) in (0,nexty-i) or (nexty,nextx)==(x-1,y-1):
return True
return False
def queens(num=8,board=[]):
for pos in range(num):
if not conflict(board,pos):
if len(board)==num-1:
yield [pos]
else:
for result in queens(num,board+[pos]):
yield [pos]+result
x,y=input().split()
x,y=int(x),int(y)
print(len(list(queens(8))))
```