How to optimize Knight's tour algorithm?

前端 未结 4 874
难免孤独
难免孤独 2021-01-30 18:40

I code the Knight\'s tour algorithm in c++ using Backtracking method. But it seems too slow or stuck in infinite loop for n > 7 (bigger than 7

4条回答
  •  我在风中等你
    2021-01-30 18:55

    this is a new solution:

    in this method, using the deadlock probability prediction at the next movement of the knight in the chessboard, a movement will be chose that it’s tending to the deadlock probability is less than the other ones, we know at the first step this deadlock probability is zero for every cells and it will be changed gradually. The knight in the chessboard has between 2 and 8 moves, so each cells has predetermined value for next move.

    Selecting the cells that have less available movement is best choice because it will tend to the deadlock in the future unless it is filled. There is an inverse relationship between allowed movement number and reach an impasse. the outer cells is in the highest priority, As regards in a knight's tour problem the knight has to cross a cell only once, these value will be changed gradually in future travels. Then in the next step a cell will be chose that has these conditions

    1. The number of its adjacent empty cells is less than others, or in the other words the probability to be filled is more
    2. After selecting, the adjacent houses doesn’t going to deadlock

    you can read my full article about this problem here Knight tour problem article

    and you can find the full source from here Full Source in GitHub

    I hope it will be useful

提交回复
热议问题