[Python] Python multiprocessing, Knight's Tour [closed]

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to brute-force calculate all possible paths for a knight's tour.

    num_size = 5
    path_list = []

    DirX = [2, 1, -1, -2, -2, -1, 1, 2]
    DirY = [1, 2, 2, 1, -1, -2, -2, -1]

    def isSafe(i, j, n, Board):
    return i >= 0 and j >= 0 and i < n and j < n and Board[j] == 0

    isPossible = False

    def knightTour(ChessBoard, path, N, x, y, visited=1):
    global isPossible

    ChessBoard[x][y] = visited

    if visited==N*N :
    isPossible = True
    global path_list
    path_list.append(path)

    ChessBoard[x][y] = 0
    return

    for i in range(8):
    newX = x + DirX
    newY = y + DirY
    if isSafe(newX, newY, N, ChessBoard) and not ChessBoard[newX][newY]:
    knightTour(ChessBoard, path+[(newX,newY)], N, newX, newY, visited + 1)

    ChessBoard[x][y] = 0

    if __name__ == "__main__":
    ChessBoard = np.array([[0 for j in range(num_size)] for i in range(num_size)])
    knightTour(ChessBoard, [(0,0)], num_size, 0, 0)


    I thought I could use multiprocessing to speed up the knightTour function but after having refactored the moves into a function on the outer level, multiprocessing.Pool and ProcessPoolExecutor gave me errors and ThreadPoolExecutor didn't even start. What is the solution?

    Continue reading...

Compartilhe esta Página