I realize that there’re many implementations of Knight’s tour problem suggested across webs, without jumping to those suggested solutions (most in Java, I’m not proficient in Java), I managed to implement one myself using Python. It works and returns some results.
A knight’s tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight’s move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.
board = Board(4,4) a = list(dfs_search([0,0], tuple()))
# a ([0, 0], [2, 1], [3, 3], [0, 2], [1, 0], [3, 1], [1, 2], [2, 0], [3, 2], [1, 1], [3, 0], [2, 3], [0, 3], [2, 2], [0, 1], [1, 3])
Most of the outputs are correct, which seems that the recursive function is actually working. The last few vertex, transit from
[3,0] is problematic. I’ve spent hours debugging trying to resolve it, but failed. As the last resort, I bring the question here, hoping anyone could shed some light to my implementation.
This is my implemntaion of Knight’s tour problem using recusive functions.
def dfs_search(start, path): if not path: path = (start,) if len(path) == board.goal: yield path for next_cell in board.avaiable_moves(start): if next_cell not in path: path += (next_cell,) yield from dfs_search(next_cell, path) class Board(object): def __init__(self, width, length): self.width = width self.length = length self.goal = width * length def is_legit_move(self, cell): row, col = cell return (row >= 0 and row <= self.width-1) and (col >=0 and col <= self.length-1) def avaiable_moves(self, cell): ret =  r, c = cell tmp = [[r+1, c-2], [r+2, c-1], [r+2, c+1], [r+1, c+2],\ [r-1, c-2], [r-2, c-1], [r-2, c+1], [r-1, c+2]] for elem in tmp: if self.is_legit_move(elem): ret.append(elem) return ret
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!