2

I'd just like to check my logic here...

I wrote code to solve the Knight's Tour and it works well for 8x8 boards starting the Knight at any square.

But... on a 5x5 board I show no solution possible when starting at square (0, 1).

What I tried for 5x5 starting the Knight at Row 0, Col 1:

  1. Warnsdorff's path
  2. Added Roth (tie breakers based on Euclidean distance from center).

Since those did not produce a solution I did code that is just basic recursion with backtracking to test every possible path -- also no solution found when starting a 5x5 on 1, 0.

I looked everywhere for a list of exhaustive solutions to the 5x5 board but found none.

Is it that there just is no solution for 5x5 when starting at square 0, 1?

Thank you!

false
  • 10,264
  • 13
  • 101
  • 209
Matt M.
  • 812
  • 3
  • 16
  • 24

5 Answers5

4
                             1   2   3   4     5

                          1 304  0   56   0    304

                          2  0   56   0   56    0

                          3  56  0   64   0    56

                          4  0   56    0  56   0

                          5 304   0   56   0   304

This might help.If knights starts at (1,1) there will be 304 possible knights tour and if it starts at (1,2) then there will be NO knights tour.Similarly if knight starts at (3,3) then there are 64 possible knights tour.

  • 2
    How does "this" help? Do you have any source for that matrix? – Nico Haase Jun 19 '18 at 11:17
  • 2
    I just said that it might give clearance for the (question) that whichever method he uses he cannot find solution if knight starts at particular square and i dont know how to get that matrix but i got this from one of the link on wikipedia.https://oeis.org/A165134 – Tejkiran Yabaku Jun 20 '18 at 05:30
2

Correct, there is no solution when you start at any of the squares adjacent to a corner square.

Joe
  • 88
  • 6
1

By a simple coloring argument, you must start on a square the same color as a corner.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

Here's an English explanation for why no knight's tour on a 5x5 board can start at (0,1). This is the "coloring" argument that Ben Voigt alludes to.

Imagine that you are coloring the board like a checkerboard, where the center square is white. Then the board has 13 white squares and 12 black squares.

Every time a knight moves, it changes the color of its square. So if you start on a black square, after 1 move you'll be on a white square (with your original square used up), after 2 moves you'll be on a black square (with 1 black and 1 white square used up), after 3 moves you'll be on a white square (with 2 blacks and 1 white square used up).

Continuing this pattern, after 24 moves, you will have used up 12 black squares, 12 white squares, and your knight will be on a white square.

But the only unused square is white!

It's impossible for you to get to that last square, because it's the same color as the square your knight is on.

onigame
  • 214
  • 3
  • 10
1
import numpy as np
import matplotlib.pyplot as plt
import copy
import time

grid_points = []
grid_nom = set()
for i in range(5):
    for j in range(5):
        grid_nom.add(f"A_{i+1}_{j+1}")
        grid_points.append([f"A_{i+1}_{j+1}", i+1, j+1])

origin = "A_2_2"
chain_propogation = [[origin]]
chain_length = 1
grid_num = len(grid_nom)

start = time.time()
while chain_length <= grid_num-1:
    new_chain_propogation = []
    for chain in chain_propogation:
        mother_grid = copy.copy(grid_nom)
        last_node = chain[-1]
        rest_grid = mother_grid.difference(set(chain))
        children = []
        for point in rest_grid:
            if (int(last_node.split('_')[1])-int(point.split('_')[1]))*(int(last_node.split('_')[2])-int(point.split('_')[2])) in [2, -2]:
                children.append(point)
        if len(children) == 0:
            pass
        else:
            for child in children:
                new_chain = copy.copy(chain)
                new_chain.append(child)
                new_chain_propogation.append(new_chain)

    chain_propogation = new_chain_propogation
    chain_length += 1
end = time.time()
print(f'Starting from {origin} in this 5x5 takes {end-start} seconds.')
print(f'There are {len(new_chain_propogation)} solutions as follows: {new_chain_propogation}.')

## example of one tour route
n = 1
tour_0 = []
for point in new_chain_propogation[n-1]:
    tour_0.append([int(point.split('_')[1]), int(point.split('_')[2])])

fig, ax = plt.subplots(figsize=(4, 3))
grid_arry = np.array(grid_points)
ax.plot(np.array(tour_0).T[0], np.array(tour_0).T[1], color="r")
ax.scatter(np.array(tour_0).T[0], np.array(tour_0).T[1], marker = "o", color="b")
BaZingo
  • 11
  • 2