trouble with knights tour (recursion)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

trouble with knights tour (recursion)

 
0
  #1
Sep 20th, 2005
I'm new to recursion and object oriented programming, but we have been assigned the knight's tour problem. This is where the knight's piece has to travel to every space on a chess board and land on each spot only once. I pretty much have my program done, except i'm having a small problem. My program runs fine, but when the board prints out, some of the spaces have the same "move" number in them. I can't figure out why this is happening. I'm thinking there may be a small problem in my recursion but i'm not really sure. I attached my code to the post(atleast i think i did). If anyone would take the time to go over it, I would greatly appreciate it. Thanks in advance for any help you can give.
Attached Files
File Type: cpp knightsTour.cpp (3.5 KB, 158 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 104
Reputation: Alvein is an unknown quantity at this point 
Solved Threads: 4
Alvein's Avatar
Alvein Alvein is offline Offline
Junior Poster

Re: trouble with knights tour (recursion)

 
0
  #2
Sep 21st, 2005
After some step-into's, I see your program backtracks well, but the "dead-ends" are not removed. Though it can move forward, the previous cells are not cleared, so it show duplicated numbers in the board.

Let's say the algorithm is ok, but the problem is C++ and the way you expect things work. You want to pass the whole board as a parameter. Though in a pseudo-code level this could create a new instance of the board on every recursive call (making possible to restore a previous instance with backtracking), in C/C++ you're not passing the board but a reference (pointer) to it. That means you're using only one instance, so the board won't be restored to a previous state when the program moves back.

Try boardType[row][column] = 0 in the last else just before "go back recursively".
Damn it, I still dream of her....:(
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: trouble with knights tour (recursion)

 
0
  #3
Sep 21st, 2005
thanks for the reply.
Yes, i had noticed that and actually tried entering that line, but when i do it seems to go into an infinite loop and just keeps printing out the board.
I even tried entering the line where a validMove = false, but that only returned an empty board. I'm so lost
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 23
Reputation: RehabReda is an unknown quantity at this point 
Solved Threads: 0
RehabReda RehabReda is offline Offline
Newbie Poster

Re: trouble with knights tour (recursion)

 
0
  #4
Sep 25th, 2009
i have also a problem with this i am using backtracking but i don't know it doesn't work right it take alot of time it is an ACM problem here is the code please if anyone have any idea what is wrong in this code please inform
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct Square
  9. {
  10. int Row, IColumn;
  11. char Column;
  12. }Source, Target;
  13. int count1;
  14. bool Visited[9][9];
  15. vector<int> MinNumberOfKnightMoves;
  16.  
  17. int NRow[] = {-2, -2, -1, -1, 1, 1, 2, 2};
  18. int NColumn[] = {-1, 1, -2, 2, -2, 2, -1, 1};
  19.  
  20. bool isValid(int Row, int Column)
  21. {
  22. return (Row > 0 && Row < 9 && Column > 0 && Column < 9);
  23. }
  24.  
  25. void KnightMoves(int NumberOfMoves, int Row, int Column)
  26. {
  27.  
  28.  
  29. if(NumberOfMoves==64)
  30. return;
  31. if(Row == Target.Row && Column == Target.IColumn)
  32. {
  33. MinNumberOfKnightMoves.push_back(NumberOfMoves);
  34.  
  35. return;
  36. }
  37.  
  38.  
  39.  
  40. for(int i = 0; i < 8; i ++)
  41. {
  42. if(isValid( Row + NRow[i], Column + NColumn[i]) && !Visited[ Row + NRow[i]][Column + NColumn[i]])
  43. {
  44. Visited[Row][Column] = true;
  45.  
  46. KnightMoves(NumberOfMoves + 1, Row + NRow[i], Column + NColumn[i]);
  47.  
  48. Visited[Row][Column] = false;
  49. }
  50.  
  51. }
  52.  
  53. }
  54.  
  55. int main()
  56. {
  57.  
  58. string square;
  59.  
  60. while(cin >> square)
  61. { count1 =0;
  62. Source.Column = square[0];
  63. Source.IColumn = square[0] - 'a' + 1;
  64. Source.Row = square[1] - '0';
  65.  
  66. cin >> square;
  67.  
  68. Target.Column = square[0];
  69. Target.IColumn = square[0] - 'a' + 1;
  70. Target.Row = square[1] - '0';
  71.  
  72. for(int i = 0; i < 9; i ++)
  73. for(int j = 0; j < 9; j ++)
  74. Visited[i][j] = false;
  75.  
  76. KnightMoves(0, Source.Row, Source.IColumn);
  77. sort(MinNumberOfKnightMoves.begin(), MinNumberOfKnightMoves.end());
  78.  
  79. cout << "To get from " << Source.Column << Source.Row << " to "
  80. << Target.Column << Target.Row <<" takes "
  81. << MinNumberOfKnightMoves[0] << " knight moves." << endl;
  82.  
  83. MinNumberOfKnightMoves.clear();
  84. }
  85. return 0;
  86. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC