Knights Tour Problem

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

Join Date: Mar 2008
Posts: 21
Reputation: buddha527 is an unknown quantity at this point 
Solved Threads: 0
buddha527's Avatar
buddha527 buddha527 is offline Offline
Newbie Poster

Knights Tour Problem

 
0
  #1
May 16th, 2008
I have a programming assignment to write the Knight's Tour. I have completed the code using simple recursive calls, my problem seems to be if the user enters any starting location the program takes longer then 45 minutes to complete and I am not sure if it even completes because after 45 minutes of having the program think I had to leave the lab and go to class. If anyone has any suggestions that can make it compute faster they will be greatly appreciated. I have attached the following code below. In case that attachments do not work I have also posted my code below:

  1. BOARD.H
  2.  
  3. #ifndef BOARD_H // if the header file board.h is not defined
  4. #define BOARD_H // then start defining board.h with the below code
  5.  
  6.  
  7. #include <iostream> // allows the use of input and outputs in the program
  8.  
  9. using std::cout; // allows the use of cout from the standard library without having to put std:: each time
  10. using std::cin; // allows the use of cin from the standard library without having to put std:: each time
  11. using std::endl; // allows the use of endl from the standard library without having to put std:: each time
  12.  
  13. #include <iomanip> // allows the use of display manipulators in the program
  14.  
  15. using std::setw; // allows the use of setw from the standard library without having to put std:: each time
  16.  
  17. const int SUCCESS = 0; // indicates that the program has completed successfully
  18.  
  19. class Board{ // creates a class called Board
  20.  
  21. public: // everything below here the user has access to and can change
  22.  
  23. // Constructors/De-constructors
  24. Board(); // default constructor
  25. ~Board(); // de-constructor
  26.  
  27. // Member Functions
  28.  
  29. void printBoard(); // function that prints out the 2D chess board
  30. bool knightTour(int, int, int); // function that takes to integers and moves does the knight tour
  31.  
  32. private: // enverything below here the user does not have direct access to
  33.  
  34. int row;
  35. int col;
  36. int count; // creates and sets the variable count to 1; this will be the move number of the knight's tour
  37.  
  38. int board[8][8];
  39.  
  40. }; // end of class Board
  41.  
  42. #endif // stops defining board.h
  43.  
  44. **********************************
  45. MAIN.CPP
  46.  
  47. // PRINTS OUT CORNERS FAST
  48.  
  49.  
  50. #include "board.h" // allows the use of everything inside the header file
  51.  
  52. int main(){ // start of executable program
  53.  
  54. int row; // creates a variable row to hold a user input
  55. int col; // creates a variable col to hold user input
  56. int count = 1; // creates and sets the variable count to 1; this will be the move number of the knight's tour
  57.  
  58. Board p; // creates an object of class Board called p for its use to call the printBoard function
  59. Board k; // creates an object of class Board called k; k will be used to call the knightTour function
  60.  
  61. cout << "Welcome to our Knight's Tour Program" << endl << endl; // display message shown to the user to let them know what the program is
  62.  
  63. p.printBoard(); // using the created class object p to call the printBoard function
  64.  
  65. cout << "The first number you enter will be the row the second number will be column" << endl;
  66. cout << "Enter the starting location of the knight [from (0,0) to (7,7)]: "; // display message to user asking for input
  67. cin >> row >> col; // reads the user input into the created variable slots
  68.  
  69. cout << "\nTest Echo Input\n" << "row: " << row << " " << "col: " << col << endl << endl; // test to make sur ethe user inputs are actually read into the program
  70.  
  71. /*if( k.knightTour(row,col,count) ){
  72.  
  73. p.printBoard();
  74.  
  75. } // end of if*/
  76.  
  77. if( k.knightTour(row,col,count) ){
  78.  
  79. k.printBoard();
  80.  
  81. } // end of if
  82. else{
  83.  
  84. cout << "No path was found" << endl << endl;
  85.  
  86. } // end of else
  87.  
  88. return SUCCESS;
  89.  
  90. } // end of main
  91.  
  92. ***************************
  93. BOARD.CPP
  94.  
  95. #include "board.h" // allows the use of everything inside the header file
  96.  
  97. //==================
  98. // OUTPUT(s): None
  99. //
  100. // INPUT(s): None
  101. //
  102. //==================
  103.  
  104. Board::Board(){ // this is the default constructor for the class Board
  105.  
  106. for( row = 0; row <= 7; row++ ){
  107.  
  108. for( col = 0; col <= 7; col++ ){
  109.  
  110. board[row][col] = 0;
  111.  
  112. } // end of inner for
  113.  
  114. } // end of outer for
  115.  
  116. } // end of constructor
  117.  
  118. //==================
  119. // OUTPUT(s): None
  120. //
  121. // INPUT(s): None
  122. //
  123. //===================
  124.  
  125. Board::~Board(){ // this is the de-constructor for the class Board
  126. } // end of de-constructor
  127.  
  128. //============================================
  129. // OUTPUT(s): Prints out the 2D chess board
  130. //
  131. // INPUT(s): None
  132. //
  133. //============================================
  134.  
  135. void Board::printBoard(){ // function header for the function printBoard of class Board; taking no arguments
  136.  
  137. for( int i = 0; i <= 7; i++ ){ // sets an integer i to 0 and as long as i is less than or equal to 7 below is run, after below finished i is increased by 1
  138.  
  139. for( int j = 0; j <= 7; j++ ){ // sets an integer j to 0 and as long as j is less than or equal to 7 below is run; after below is finished running j is increased by 1
  140.  
  141. cout << setw(3) << board[i][j]; // takes the current location in the 2D array board and prints it to the screen; should print out a 8 by 8 board of 0's
  142.  
  143. } // end of inner for
  144.  
  145. cout << "\n\n"; // prints out blank lines for spacing
  146.  
  147. } // end of outer for
  148.  
  149. } // end of member function printBoard for class Board
  150.  
  151. //====================
  152. // OUTPUT(s): ?
  153. //
  154. // INPUT(s): User inputs for the starting location of the knight
  155. //
  156. //====================
  157.  
  158. bool Board::knightTour(int row, int col, int count){ // function header for the function knightTour; taking the integer arguments of row and col that the user has entered
  159.  
  160. bool pathFound = false;
  161.  
  162. if( (row >= 0 && row <= 7) && (col >= 0 && col <= 7) ){ // if 1
  163.  
  164. if( board[row][col] == 0 ){ // if 2
  165.  
  166. board[row][col] = count;
  167.  
  168. if( count == 64 ){
  169.  
  170. pathFound = true;
  171.  
  172. } // end of if
  173.  
  174. if(!pathFound){ // if 3
  175.  
  176. pathFound = knightTour( row - 2, col + 1, count + 1 );
  177.  
  178. if( !pathFound ){
  179.  
  180. pathFound = knightTour( row - 1, col + 2, count + 1 );
  181.  
  182. } // end of if
  183.  
  184. if( !pathFound ){
  185.  
  186. pathFound = knightTour( row + 1, col + 2, count + 1 );
  187.  
  188. } // end of if
  189.  
  190. if( !pathFound ){
  191.  
  192. pathFound = knightTour( row + 2, col + 1, count + 1 );
  193.  
  194. } // end of if
  195.  
  196. if( !pathFound ){
  197.  
  198. pathFound = knightTour( row + 2, col - 1, count + 1 );
  199.  
  200. } // end of if
  201.  
  202. if( !pathFound ){
  203.  
  204. pathFound = knightTour( row + 1, col - 2, count + 1 );
  205.  
  206. } // end of if
  207.  
  208. if( !pathFound ){
  209.  
  210. pathFound = knightTour( row - 1, col - 2, count + 1 );
  211.  
  212. } // end of if
  213.  
  214. if( !pathFound ){
  215.  
  216. pathFound = knightTour( row - 2, col - 1, count + 1 );
  217.  
  218. } // end of if
  219.  
  220. if( !pathFound ){
  221.  
  222. board[row][col] = 0;
  223.  
  224. } // end of if
  225.  
  226. } // end of if 3
  227.  
  228. } // end of inner if 2
  229.  
  230. } // end of outer if 1
  231.  
  232. return (pathFound);
  233.  
  234. }; // end of member function knightTour for class Board
Attached Files
File Type: cpp board.cpp (3.2 KB, 13 views)
File Type: h board.h (1.5 KB, 15 views)
File Type: cpp main.cpp (1.6 KB, 11 views)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Knights Tour Problem

 
0
  #2
May 16th, 2008
I posted an Identical thread in c fourum & but mine was faster though. It takes a few minutes to find a combination. & then to improve the performance, I'll give the same link I recived Warnsdorff's Algorithm
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 21
Reputation: buddha527 is an unknown quantity at this point 
Solved Threads: 0
buddha527's Avatar
buddha527 buddha527 is offline Offline
Newbie Poster

Re: Knights Tour Problem

 
0
  #3
May 16th, 2008
Thank you I will check it out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: bulg is an unknown quantity at this point 
Solved Threads: 1
bulg bulg is offline Offline
Newbie Poster

Re: Knights Tour Problem

 
0
  #4
May 16th, 2008
This problem is to prove that recursion is O(x!) right? Maybe your solution works fine: http://en.wikipedia.org/wiki/Big_O_notation

It might help a little in certain cases to implement Schwenk's Theorem ( w/ 4 if statements ) or, if you want to go at it, check out the link at the bottom (http://portal.acm.org/citation.cfm?id=363463)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knights Tour Problem

 
0
  #5
May 16th, 2008
too bad most people on here don't have membership to ACM.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: bulg is an unknown quantity at this point 
Solved Threads: 1
bulg bulg is offline Offline
Newbie Poster

Re: Knights Tour Problem

 
0
  #6
May 16th, 2008
The first reply is actually a better page for this question, so I'll take that to mean you just like posting flamebait.
Last edited by bulg; May 16th, 2008 at 6:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knights Tour Problem

 
0
  #7
May 16th, 2008
what, does everyone here have an ACM membership?

am i the only one who doesnt?

because i was interested in your links, so i went to check it out (the one about Hamilton Paths)

and well, guess what? I need to pay $99.00 in order to access your article.

so, instead of you getting all clenched up at the slightest bit of justified criticism, maybe you ought to consider your audience when you post links.

I mean, yeah, I could go and post all sorts of links to IEEE peer-reviewed journal articles... but i dont. ... for obvious reasons.

and by the way, great way to introduce yourself. welcome to the community.



.
Last edited by jephthah; May 16th, 2008 at 7:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: bulg is an unknown quantity at this point 
Solved Threads: 1
bulg bulg is offline Offline
Newbie Poster

Re: Knights Tour Problem

 
0
  #8
May 16th, 2008
[edit] this doesn't belong on this board [/edit]
Last edited by bulg; May 16th, 2008 at 7:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knights Tour Problem

 
0
  #9
May 16th, 2008
Originally Posted by bulg View Post
[edit] this doesn't belong on this board [/edit]

and neither do those $99.00 links



.
Last edited by jephthah; May 16th, 2008 at 8:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: bulg is an unknown quantity at this point 
Solved Threads: 1
bulg bulg is offline Offline
Newbie Poster

Re: Knights Tour Problem

 
0
  #10
May 16th, 2008
Here you go:
<snipped link>
Last edited by Ancient Dragon; May 16th, 2008 at 9:18 pm. Reason: snipped link
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