在 n×n 棋盘上,从左上到右,每个方格上都有 1 个棋子。我想要一对棋子向下移动 1 个方格,直到到达最后一排
的预期输出 的预期输出import numpy as np
def move_checkers(n):
if n < 4:
print("Error: n should be greater than or equal to 4.")
return
#checkerboard with checkers on the main diagonal
board = np.array([[0] * n for _ in range(n)])
for i in range(n):
board[i][i] = 1
# print(board)
print("initial = ")
for row in board:
print(row)
print()
# Move checkers down until they reach the bottom row
moves = 0
for j in range(n):
i = 0
while i < n-1:
if board[i][j] == 1 and board[i+1][j] == 0:
board[i][j], board[i+1][j] = board[i+1][j], board[i][j]
moves += 1
print(board[i][j], board[i+1][j] , "and", board[i+1][j], board[i+1][j])
i = 0 # start from the top again
print("move = ",moves)
for row in board:
print(row)
print()
else:
i += 1
# Check if all checkers are on the bottom row
for j in range(n):
if board[n-1][j] == 0:
print("Cannot move all checkers to bottom row for n =", n)
return
print("All checkers moved to bottom row for n =", n)
print("Total number of moves =", moves)
move_checkers(4)
现在我一次移动每个棋子,但我想要一对棋子在 n x n 上移动一次 董事会。