Author Topic: Knight Moves  (Read 22108 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Knight Moves
« on: October 13, 2018, 11:34:18 am »
AKA Knight's Tour at Rosetta:
Code: QB64: [Select]
  1. 'Knight Moves 3 Recurring.txt for JB v2.0 B+ started 2018-10-06
  2. DEFINT A-Z
  3. 'OK try some recusion
  4. DIM SHARED counter&&
  5.  
  6. DIM SHARED dxa(1 TO 8)
  7. DIM SHARED dya(1 TO 8)
  8.  
  9. DIM i, startx, starty, beenHere
  10. DIM board$
  11.  
  12. DATA 2,1,-1,-2,-2,-1,1,2
  13. FOR i = 1 TO 8
  14.     READ dxa(i)
  15. DATA 1,2,2,1,-1,-2,-2,-1
  16. FOR i = 1 TO 8
  17.     READ dya(i)
  18.  
  19. FOR i = 1 TO 64
  20.     board$ = board$ + CHR$(98) 'using chr$(98) for not visited yet signal
  21. 'PRINT board$
  22.  
  23. startx = 1: starty = 1: beenHere = 0
  24. MoveKnight startx, starty, board$, beenHere
  25.  
  26. SUB MoveKnight (x, y, board$, beenHere)
  27.     'recursive procedure: from this x, y position
  28.     'first check if position is OK to move into, if so
  29.     '    record the position in the board
  30.     '    then see if the board is complete, show soultion if so
  31.     '    else find new positions to move to by recursive calls
  32.     'else dead end exit
  33.     'convert x, y to a position in a string 64 characters long
  34.     DIM cy, cx, cBoard$, cBeenHere
  35.     cx = x: cy = y: cBoard$ = board$: cBeenHere = beenHere
  36.     DIM position, m, dx, dy, wate$
  37.     position = (cy - 1) * 8 + cx
  38.     IF MID$(cBoard$, position, 1) = CHR$(98) THEN 'OK to move here
  39.         'PRINT board$
  40.         'add position to board string
  41.         cBeenHere = cBeenHere + 1
  42.         MID$(cBoard$, position, 1) = CHR$(cBeenHere)
  43.  
  44.         'check board finished ha!
  45.         IF INSTR(cBoard$, CHR$(98)) > 0 THEN 'not done
  46.  
  47.             'progress report
  48.             counter&& = counter&& + 1
  49.             IF counter&& MOD 10000 = 0 THEN
  50.                 'IF counter&& MOD 1 = 0 THEN
  51.                 ShowBoard cBoard$
  52.                 '_DELAY .20 'really will bog down processing!
  53.             END IF
  54.  
  55.             FOR m = 1 TO 8
  56.                 dx = dxa(m)
  57.                 dy = dya(m)
  58.                 'check now that the recursive call is in bounds
  59.                 IF cx + dx >= 1 AND cx + dx <= 8 THEN
  60.                     IF cy + dy >= 1 AND cy + dy <= 8 THEN
  61.                         MoveKnight cx + dx, cy + dy, cBoard$, cBeenHere
  62.                     END IF
  63.                 END IF
  64.             NEXT
  65.         ELSE 'we are done and have a solution to show!
  66.             ShowBoard cBoard$
  67.             PRINT
  68.             PRINT "    That's All Folks!"
  69.             END
  70.         END IF
  71.     END IF
  72.  
  73. SUB ShowBoard (b$)
  74.     DIM row, col, visit
  75.     CLS
  76.     PRINT counter&&
  77.     FOR row = 0 TO 7
  78.         FOR col = 1 TO 8
  79.             LOCATE row + 3, col * 4
  80.             visit = ASC(MID$(b$, row * 8 + col, 1))
  81.             IF visit = 98 THEN PRINT "    " ELSE PRINT RIGHT$("    " + STR$(visit), 4)
  82.         NEXT
  83.     NEXT
  84.  

A better version of this can be made by using a recursive function returning 1 if solved and 0 if not, then you would NOT have to carry around a copy of the board.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Knight Moves
« Reply #1 on: October 15, 2018, 04:50:48 pm »
Well, that's got that covered.

Offline Thunder Cat

  • Newbie
  • Posts: 2
    • View Profile
Re: Knight Moves
« Reply #2 on: April 17, 2020, 07:59:23 am »
This version would run much faster if a modification was made for corner movements. Specifically, there is only one way into and out of a corner square. So when you get to C2, the next two moves are always A1 and B3. Or, if you approach from B3, the next two moves are always A1 and C2. In the version shown, you are investigating all the possibilities ignoring this fact, so you spend a huge amount of time investigating paths that lead to nowhere, because you have to do the search for each of the four corners

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #3 on: April 17, 2020, 01:50:19 pm »
This version would run much faster if a modification was made for corner movements. Specifically, there is only one way into and out of a corner square. So when you get to C2, the next two moves are always A1 and B3. Or, if you approach from B3, the next two moves are always A1 and C2. In the version shown, you are investigating all the possibilities ignoring this fact, so you spend a huge amount of time investigating paths that lead to nowhere, because you have to do the search for each of the four corners

Nope don't think so, you are suggesting more conditional testing which is sure to slow down code.

Of course you could prove me wrong ;-))

For me, this is the right amount of code with a timely solution.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Knight Moves
« Reply #4 on: April 18, 2020, 08:12:43 pm »
What exactly is this doing?
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #5 on: April 18, 2020, 08:33:33 pm »
Knight covers chess board starting at #1 spot top left without repeating a spot:
http://rosettacode.org/wiki/Knight%27s_tour

 
Knight Moves.PNG


Sure probably are smarter ways but this is done in under a minute so really, how much time do you want to spend on this?
« Last Edit: April 18, 2020, 08:39:02 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Knight Moves
« Reply #6 on: April 22, 2020, 08:35:49 am »
Hi Bplus
Cool

about goal I'm a bit confusing....
Quote
you have a standard 8x8 chessboard, empty but for a single knight on some square. Your task is to emit a series of legal knight moves that result in the knight visiting every square on the chessboard exactly once. Note that it is not a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position.
my ability to understand gets that I must move the Knight on each of 64 cells of chessboard touching one time the single cell, because I cannot pass 2 times on the same cell.

while the image doesn't show this but a simple tour on the chessboard of 24 steps in which the knight uses one time a cell to travel
http://rosettacode.org/mw/images/c/cd/Knight%27s_tour_7x7.png


PS: I see you follow the main stream of output ... no graphic output or Algebrical output but ASCII rappresentation of chessboard with the number of move in each cell. A good comparison.
Quote
If textual, squares should be indicated in algebraic notation. The output should indicate the order in which the knight visits the squares, starting with the initial position. The form of the output may be a diagram of the board with the squares numbered according to visitation sequence, or a textual list of algebraic coordinates in order, or even an actual animation of the knight moving around the chessboard.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #7 on: April 22, 2020, 12:52:43 pm »
Thanks TempodiBasic, maybe you are confused by the instruction that the knight does not have to return to where it started like a "closed" loop or circuit would be.

You know, since this topic has been reawakened, I have a thought how it might be done faster. I will update if experiment is successful.

Update: Well I learned something kind of ugly about this code, it won't stop if you put EXIT SUB in place of END in the subroutine. ;-(( yikes it goes Energizer bunny on me, but I have that fixed with an extra shared variable and check at start of SUB, a hack job. Now I can analyse solution and continue with experiment.
« Last Edit: April 22, 2020, 02:32:38 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #8 on: April 23, 2020, 12:50:38 pm »
Well experiment was dismal failure; guess I had pretty naive idea how this thing worked; see conclusions in code comments. So I guess if I were to pursue this further: study solution for a pattern and yes, use a bunch of IF blocks to test where Knight is on board specially edges and corners.

Code: QB64: [Select]
  1. 'Revisit KnightMoves for QB64 started from Knight Moves 3 Recurring.txt for JB v2.0 B+ started 2018-10-06
  2. ' 2020-04-21 Overhaul MoveKnight SUB so can analyse solution after found.
  3. ' 2020-04-22 Cleanup code in MoveKnight SUB add timer and analyse moves. Try running most used moves first to try
  4. ' to speed up and reduce recursive calls, backfired completely!!!  No solution found let alone using less iterations.
  5. ' Conclusion:
  6. ' The Knights Tour problem is very sentive to move order AND where the Knight starts.
  7. ' For instance, starting the Knight at 8, 8 instead of 1, 1 fails to find a solution after running
  8. ' millions and millions of calls. There must be a pattern needed to run the Knight to cover the board.
  9. ' So yes, might run the Knight according to where it is on board, specially edges and corners.
  10.  
  11. DEFINT A-Z
  12. _TITLE "Knight Moves Extended with Analysis" 'b+ 2020-04-22 looking at moves made on board,
  13. DIM SHARED counter&&, soln$, dx(1 TO 8), dy(1 TO 8)
  14. DIM i, board$, start!, tTime!, lastx, lasty, x, y, position, dx, dy, j, high, savej
  15.  
  16. DATA 2,1,-1,-2,-2,-1,1,2
  17. FOR i = 1 TO 8
  18.     READ dx(i)
  19. DATA 1,2,2,1,-1,-2,-2,-1
  20. FOR i = 1 TO 8
  21.     READ dy(i)
  22. board$ = STRING$(64, "b") 'using chr$(98) for not visited yet signal
  23. start! = TIMER(.001)
  24. MoveKnight 1, 1, board$, 0
  25. tTime! = TIMER(.001) - start!
  26. ShowBoard soln$
  27. PRINT tTime!; "secs"
  28. '=========================================== end of original code reworked
  29.  
  30. PRINT "Press any for analysis"
  31. PRINT: PRINT "Analyzing..."
  32. DIM moves(1 TO 8)
  33. FOR i = 1 TO 64
  34.     'find i find the dx, dy move from last position to current position
  35.     position = INSTR(soln$, CHR$(i))
  36.     x = position MOD 8: y = INT(position / 8) + 1
  37.     IF x = 0 THEN x = 8: y = y - 1
  38.     'PRINT i, x, y
  39.     'INPUT "ok ..."; wate$   'ok working
  40.     IF i > 1 THEN
  41.         dx = x - lastx: dy = y - lasty
  42.         FOR j = 1 TO 8
  43.             IF dx(j) = dx AND dy(j) = dy THEN moves(j) = moves(j) + 1: EXIT FOR
  44.         NEXT
  45.     END IF
  46.     lastx = x: lasty = y
  47. PRINT "Here is listing of how many times a move was made:"
  48. FOR i = 1 TO 8
  49.     PRINT i, moves(i)
  50. PRINT "Lets sort this list so most used move is first and least is last and rerun the soln to see if we saved time and interations."
  51. DIM newDX(1 TO 8), newDY(1 TO 8)
  52. FOR i = 1 TO 8
  53.     'search moves for highest number
  54.     high = 0: savej = 0
  55.     FOR j = 1 TO 8
  56.         IF moves(j) > high THEN high = moves(j): savej = j
  57.     NEXT
  58.     newDX(i) = dx(savej): newDY(i) = dy(savej): moves(savej) = 0 'remove j from search candidates
  59. PRINT "New dx, dy order:"
  60. FOR i = 1 TO 8 'trading places
  61.     dx(i) = newDX(i): dy(i) = newDY(i)
  62.     PRINT dx(i), dy(i)
  63. PRINT "Press any to run recursive sub again..."
  64. 'reset and run MoveKnight again to see if save time and recursive calls
  65. soln$ = "": counter&& = 0
  66. board$ = STRING$(64, "b") 'using chr$(98) for not visited yet signal
  67. start! = TIMER(.001)
  68. MoveKnight 1, 1, board$, 0
  69. tTime! = TIMER(.001) - start!
  70. ShowBoard soln$
  71. PRINT tTime!; "secs"
  72.  
  73. SUB MoveKnight (x, y, board$, beenHere) ' copy solution of Knight's Tour to SHARED soln$ variable
  74.     ' x, y have been prechecked to be in the board, will test to see if been here (x, y) yet.
  75.     ' board$ is the current instance of chess board$ with places Knight has been already.
  76.     ' beenHere is the current move number the knight is on.
  77.  
  78.     IF soln$ <> "" THEN EXIT SUB '<<<<<<<<<<<<<<<< stop the solution has been found!
  79.     DIM cy, cx, cBoard$, cBeenHere ' copy all incoming
  80.     cx = x: cy = y: cBoard$ = board$: cBeenHere = beenHere
  81.     DIM position, m
  82.     position = (cy - 1) * 8 + cx 'convert x, y to a position in a board string 64 characters long
  83.     IF MID$(cBoard$, position, 1) = CHR$(98) THEN '  have not been here yet
  84.         cBeenHere = cBeenHere + 1 'add position to board string
  85.         MID$(cBoard$, position, 1) = CHR$(cBeenHere)
  86.         IF INSTR(cBoard$, CHR$(98)) > 0 THEN ' still empty spots so not done
  87.             counter&& = counter&& + 1
  88.             IF counter&& MOD 1000 = 0 THEN ShowBoard cBoard$ 'slows down soln by 1-2 secs
  89.             FOR m = 1 TO 8
  90.                 'check now that the recursive call is in bounds
  91.                 IF cx + dx(m) >= 1 AND cx + dx(m) <= 8 THEN
  92.                     IF cy + dy(m) >= 1 AND cy + dy(m) <= 8 THEN
  93.                         MoveKnight cx + dx(m), cy + dy(m), cBoard$, cBeenHere
  94.                     END IF
  95.                 END IF
  96.             NEXT
  97.         ELSE 'we are done and have a solution to show!
  98.             soln$ = cBoard$ ' copy soln to shared variable
  99.         END IF
  100.     END IF
  101.  
  102. SUB ShowBoard (b$)
  103.     DIM row, col, visit
  104.     CLS
  105.     PRINT counter&&; "recursive calls"
  106.     FOR row = 0 TO 7
  107.         FOR col = 1 TO 8
  108.             LOCATE row + 3, col * 4
  109.             visit = ASC(MID$(b$, row * 8 + col, 1))
  110.             IF visit = 98 THEN PRINT "    " ELSE PRINT RIGHT$("    " + STR$(visit), 4)
  111.         NEXT
  112.     NEXT
  113.     PRINT
  114.  
  115.  


Marked as best answer by bplus on April 24, 2020, 10:54:49 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #9 on: April 24, 2020, 01:45:09 pm »
OK Thunder Cat, I rewrote the whole program to only move to squares accessible to each square such that (yeah) each corner is only accessible or accesses 2 other squares but guess what, it's no faster, it runs the same amount of recursive SUB calls (and thus time as well).

BUT! I did find a much better place to start AND a much better sequence of moves to try such that, I cut the time and recursive SUB calls by 1/3!

Code: QB64: [Select]
  1. _TITLE "Knight IF Here Moves" ' b+ started 2020-04-24
  2. ' 2020-04-24 start over the Rosetta Code challenge called Knight's Tour
  3. ' try testing moves from a matrix listing possible positions from a given position
  4.  
  5. DEFINT A-Z
  6. CONST sq = 8 'try to work this for any sq size
  7. DIM SHARED km(1 TO sq * sq, 1 TO 8), dx(1 TO 8), dy(1 TO 8), counter&&, soln$
  8. 'from any of 64, km stands for knight move the first dim indexes the sq position in one variable
  9. 'the 2nd dimension lists the positions a move go goto
  10. DATA 2,1,-1,-2,-2,-1,1,2
  11. FOR i = 1 TO 8
  12.     READ dx(i)
  13. DATA 1,2,2,1,-1,-2,-2,-1
  14. FOR i = 1 TO 8
  15.     READ dy(i)
  16.  
  17. 'testLoad = 1    ' << uncomment to check the load of km(sq, moves available)  OK looks good
  18. FOR p = 1 TO sq * sq 'load our km(p, m(1) to m(8)) array
  19.     xy p, x, y 'get x, y for each square position on board
  20.     IF testLoad THEN PRINT RIGHT$("   " + _TRIM$(STR$(p)), 3); ": ";
  21.     FOR m = 1 TO 8
  22.         'see if the knight move is on the board
  23.         IF x + dx(m) >= 1 AND x + dx(m) <= sq THEN
  24.             IF y + dy(m) >= 1 AND y + dy(m) <= sq THEN
  25.                 km(p, m) = position(x + dx(m), y + dy(m))
  26.                 IF testLoad THEN PRINT RIGHT$("   " + _TRIM$(STR$(km(p, m))), 3);
  27.             ELSE
  28.                 IF testLoad THEN PRINT " 00";
  29.             END IF
  30.         ELSE
  31.             IF testLoad THEN PRINT " 00";
  32.         END IF
  33.     NEXT
  34.     PRINT
  35.  
  36. board$ = STRING$(64, "b") 'using chr$(98) for not visited yet signal'PRINT board$
  37. start! = TIMER(.001)
  38. MoveKnight 64, board$, 0
  39. tTime! = TIMER(.001) - start!
  40. ShowBoard soln$
  41. PRINT: PRINT tTime!; "secs"
  42.  
  43. SUB MoveKnight (xyPlace, board$, beenHere) ' copy solution of Knight's Tour to SHARED soln$ variable
  44.     ' x, y have been prechecked to be in the board, will test to see if been here (x, y) yet.
  45.     ' board$ is the current instance of chess board$ with places Knight has been already.
  46.     ' beenHere is the current move number the knight is on.
  47.  
  48.     IF soln$ <> "" THEN EXIT SUB '<<<<<<<<<<<<<<<< stop the solution has been found!
  49.     cBoard$ = board$
  50.     'place = position(x, y) 'convert x, y to a position in a board string 64 characters long
  51.     IF MID$(cBoard$, xyPlace, 1) = "b" THEN '  have not been here yet
  52.         MID$(cBoard$, xyPlace, 1) = CHR$(beenHere + 1)
  53.         IF INSTR(cBoard$, "b") > 0 THEN ' still empty spots so not done
  54.             counter&& = counter&& + 1
  55.             IF counter&& MOD 1000 = 0 THEN ShowBoard cBoard$ 'slows down soln by 1-2 secs
  56.             FOR m = 8 TO 1 STEP -1
  57.                 IF km(xyPlace, m) THEN 'try the place to move to
  58.                     MoveKnight km(xyPlace, m), cBoard$, beenHere + 1
  59.                 END IF
  60.             NEXT
  61.         ELSE 'we are done and have a solution to show!
  62.             soln$ = cBoard$ ' copy soln to shared variable
  63.         END IF
  64.     END IF
  65.  
  66. SUB ShowBoard (b$)
  67.     DIM row, col, visit
  68.     CLS
  69.     PRINT counter&&
  70.     FOR row = 0 TO 7
  71.         FOR col = 1 TO 8
  72.             LOCATE row + 3, col * 4
  73.             visit = ASC(MID$(b$, row * 8 + col, 1))
  74.             IF visit = 98 THEN PRINT "    " ELSE PRINT RIGHT$("    " + STR$(visit), 4)
  75.         NEXT
  76.     NEXT
  77.  
  78. ''test position function and xy sub OK good!
  79. 'FOR p = 1 TO 64
  80. '    xy p, px, py
  81. '    PRINT position(px, py); "="; px; ","; py,
  82. 'NEXT
  83.  
  84. SUB xy (place, x, y) 'note sq is const square board side size
  85.     y = INT(place / sq) + 1: x = place MOD sq
  86.     IF x = 0 THEN x = sq: y = y - 1
  87.  
  88. FUNCTION position (x, y) 'note sq is const square board side size
  89.     position = (y - 1) * sq + x
  90.  
  91.  

 
Knight IF here moves.PNG

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #10 on: April 24, 2020, 02:57:35 pm »
Just to show, it wasn't new code technique that improved the time and number of calls, here is original method with the new start point (8, 8) opposite diagonally from (1, 1) and new move order (reverse of original):
Code: QB64: [Select]
  1. _TITLE "Knight Moves" 'b+ updated version with soln$ passed to main program 2020-04-23
  2. ' 2020-04-24 cut some variable assignments from main sub to save time
  3. DEFINT A-Z
  4. DIM SHARED counter&&, soln$, dx(1 TO 8), dy(1 TO 8)
  5. DATA 2,1,-1,-2,-2,-1,1,2
  6. FOR i = 1 TO 8
  7.     READ dx(i)
  8. DATA 1,2,2,1,-1,-2,-2,-1
  9. FOR i = 1 TO 8
  10.     READ dy(i)
  11. board$ = STRING$(64, "b") 'using chr$(98) for not visited yet signal'PRINT board$
  12. start! = TIMER(.001)
  13. MoveKnight 8, 8, board$, 0
  14. ttime! = TIMER(.001) - start!
  15. ShowBoard soln$
  16. PRINT: PRINT ttime!; "sec"
  17.  
  18. SUB MoveKnight (x, y, board$, beenHere) ' copy solution of Knight's Tour to SHARED soln$ variable
  19.     ' x, y have been prechecked to be in the board, will test to see if been here (x, y) yet.
  20.     ' board$ is the current instance of chess board$ with places Knight has been already.
  21.     ' beenHere is the current move number the knight is on.
  22.  
  23.     IF soln$ <> "" THEN EXIT SUB '<<<<<<<<<<<<<<<< stop the solution has been found!
  24.     cBoard$ = board$
  25.     position = (y - 1) * 8 + x 'convert x, y to a position in a board string 64 characters long
  26.     IF MID$(cBoard$, position, 1) = "b" THEN '  have not been here yet
  27.         MID$(cBoard$, position, 1) = CHR$(beenHere + 1)
  28.         IF INSTR(cBoard$, "b") > 0 THEN ' still empty spots so not done
  29.             counter&& = counter&& + 1
  30.             IF counter&& MOD 1000 = 0 THEN ShowBoard cBoard$ 'slows down soln .25-.5 secs
  31.             FOR m = 8 TO 1 STEP -1
  32.                 'check now that the recursive call is in bounds
  33.                 IF x + dx(m) >= 1 AND x + dx(m) <= 8 THEN
  34.                     IF y + dy(m) >= 1 AND y + dy(m) <= 8 THEN
  35.                         MoveKnight x + dx(m), y + dy(m), cBoard$, beenHere + 1
  36.                     END IF
  37.                 END IF
  38.             NEXT
  39.         ELSE 'we are done and have a solution to show!
  40.             soln$ = cBoard$ ' copy soln to shared variable
  41.         END IF
  42.     END IF
  43.  
  44. SUB ShowBoard (b$)
  45.     DIM row, col, visit
  46.     CLS
  47.     PRINT counter&&
  48.     FOR row = 0 TO 7
  49.         FOR col = 1 TO 8
  50.             LOCATE row + 3, col * 4
  51.             visit = ASC(MID$(b$, row * 8 + col, 1))
  52.             IF visit = 98 THEN PRINT "    " ELSE PRINT RIGHT$("    " + STR$(visit), 4)
  53.         NEXT
  54.     NEXT
  55.  


Result:
Knight Moves with different start and move order.PNG
* Knight Moves with different start and move order.PNG (Filesize: 6.16 KB, Dimensions: 337x263, Views: 134)
« Last Edit: April 24, 2020, 02:59:10 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Knight Moves
« Reply #11 on: April 24, 2020, 07:35:44 pm »
Hi Bplus
I'm having a strange experience with latest code posted if I try the task starting from a natural position of black knight Kd7 that translated into cohordinates for your code are 4,2.
I have taken a screenshot, and I have written this post, but it is still running to solve the task....now is at 75405000
screenshot has been taken before this
 
KnightMove position 4 2.png


Do you think that some start positions have no solution for a complete tour of chessboard with one passing on each cell?
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Knight Moves
« Reply #12 on: April 24, 2020, 10:59:33 pm »
Quote
Do you think that some start positions have no solution for a complete tour of chessboard with one passing on each cell?

@TempodiBasic,
As noted already, the Knight's Tour as coded here is extremely sensitive to the starting position and the ordering of the way you test moves. I think, eventually, if you don't burn out the CPU you will get a solution but who wants to wait for (6.xxx or 7.xxx?)^64 recursive calls if you happen to start wrong near the beginning?

As you can see in the fast soln, 3 corners are covered in 21 moves, so definitely think starting at corner is really good one corner done free of charge! As Thunder Cat implied, the corners seem to be the bottle neck of the Tour, you got to go in and out just right 4 times if you don't start in one.


« Last Edit: April 24, 2020, 11:05:43 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Knight Moves
« Reply #13 on: April 25, 2020, 05:16:39 pm »
Thanks Bplus,
so the combinations are very massive and  if we start not from a corner we increase very much the work to do to get the solution. Well.
But apart the language of programming and the algorythm, it is cleared if there is a solution for any position of starting?
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Knight Moves
« Reply #14 on: April 25, 2020, 06:18:39 pm »
Just now I have controlled your Knight application that was running while I was writing in QB64.org/forum..
well it has done the work also for that position...
 
KnightMove position 4 2  2nd.png
Programming isn't difficult, only it's  consuming time and coffee