I was asked by a friend if it was possible to have a horse start in any spot on a standard chess board and jump on every square.

I have constructed an algorthim to do the crunching for me. However, it takes more than 2 hours ( I gave up waiting after 2 hours) to go through all the permutations.

Anyone have any suggestions on how to optimise the algorithm so that it takes less time?

For a start I have string concatenation in there which is slow.

If it is not possible with VB, perhaps with c++ or are there simply too many permutations?

Here is the algorithm:

VB Code:
  1. Private Sub Command1_Click()
  2. Dim timeit As Date
  3. timeit = Time
  4. Call KnightMoves(4, 1, ",41,")
  5. Debug.Print FormatDateTime(Time - timeit, vbLongTime)
  6. MsgBox "Finished"
  7. End Sub
  8.  
  9. Function KnightMoves(ByVal x, ByVal y, ByVal PrevMoves$)
  10.  Dim newx&, newy&, b1 As Boolean, b2 As Boolean, b3 As Boolean, b4 As Boolean
  11.  Dim b5 As Boolean, b6 As Boolean, b7 As Boolean
  12.  
  13.  newx = x + 2
  14.  newy = y + 1
  15.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  16.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  17.  Else
  18.     b1 = True
  19.  End If
  20.  
  21.  newy = newy - 2
  22.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  23.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  24.  Else
  25.     b2 = True
  26.  End If
  27.    
  28.  newx = newx - 4
  29.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  30.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  31.  Else
  32.     b3 = True
  33.  End If
  34.  
  35.  newy = newy + 2
  36.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  37.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  38.  Else
  39.     b4 = True
  40.  End If
  41.  
  42.  newx = newx + 1
  43.  newy = newy + 1
  44.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  45.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  46.  Else
  47.     b5 = True
  48.  End If
  49.  
  50.  newx = newx + 2
  51.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  52.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  53.  Else
  54.     b6 = True
  55.  End If
  56.  
  57.  newy = newy - 4
  58.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  59.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  60.  Else
  61.     b7 = True
  62.  End If
  63.  
  64.  newx = newx - 2
  65.  If CBool(newx > 0 And newx < 9 And newy > 0 And newy < 9) And InStr(1, PrevMoves, "," & newx & newy & ",", vbBinaryCompare) = 0 Then
  66.     Call KnightMoves(newx, newy, PrevMoves & newx & newy & ",")
  67.  Else
  68.     If b1 And b2 And b3 And b5 And b6 And b7 Then
  69.         If Len(PrevMoves) = 193 Then Debug.Print PrevMoves
  70.     End If
  71.    
  72.  End If
  73.  
  74. End Function