成对移动跳棋而不是一次移动一个跳棋

问题描述 投票:0回答:0

在 n×n 棋盘上,从左上到右,每个方格上都有 1 个棋子。我想要一对棋子向下移动 1 个方格,直到到达最后一排

8x8初始板

8x8

的预期输出

9x9

的预期输出

我得到的输出是因为我一次移动一个检查器

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 上移动一次 董事会。

python algorithm matrix logic puzzle
© www.soinside.com 2019 - 2024. All rights reserved.