| | |
Knights Tour Problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
C++ Syntax (Toggle Plain Text)
BOARD.H #ifndef BOARD_H // if the header file board.h is not defined #define BOARD_H // then start defining board.h with the below code #include <iostream> // allows the use of input and outputs in the program using std::cout; // allows the use of cout from the standard library without having to put std:: each time using std::cin; // allows the use of cin from the standard library without having to put std:: each time using std::endl; // allows the use of endl from the standard library without having to put std:: each time #include <iomanip> // allows the use of display manipulators in the program using std::setw; // allows the use of setw from the standard library without having to put std:: each time const int SUCCESS = 0; // indicates that the program has completed successfully class Board{ // creates a class called Board public: // everything below here the user has access to and can change // Constructors/De-constructors Board(); // default constructor ~Board(); // de-constructor // Member Functions void printBoard(); // function that prints out the 2D chess board bool knightTour(int, int, int); // function that takes to integers and moves does the knight tour private: // enverything below here the user does not have direct access to int row; int col; int count; // creates and sets the variable count to 1; this will be the move number of the knight's tour int board[8][8]; }; // end of class Board #endif // stops defining board.h ********************************** MAIN.CPP // PRINTS OUT CORNERS FAST #include "board.h" // allows the use of everything inside the header file int main(){ // start of executable program int row; // creates a variable row to hold a user input int col; // creates a variable col to hold user input int count = 1; // creates and sets the variable count to 1; this will be the move number of the knight's tour Board p; // creates an object of class Board called p for its use to call the printBoard function Board k; // creates an object of class Board called k; k will be used to call the knightTour function cout << "Welcome to our Knight's Tour Program" << endl << endl; // display message shown to the user to let them know what the program is p.printBoard(); // using the created class object p to call the printBoard function cout << "The first number you enter will be the row the second number will be column" << endl; cout << "Enter the starting location of the knight [from (0,0) to (7,7)]: "; // display message to user asking for input cin >> row >> col; // reads the user input into the created variable slots cout << "\nTest Echo Input\n" << "row: " << row << " " << "col: " << col << endl << endl; // test to make sur ethe user inputs are actually read into the program /*if( k.knightTour(row,col,count) ){ p.printBoard(); } // end of if*/ if( k.knightTour(row,col,count) ){ k.printBoard(); } // end of if else{ cout << "No path was found" << endl << endl; } // end of else return SUCCESS; } // end of main *************************** BOARD.CPP #include "board.h" // allows the use of everything inside the header file //================== // OUTPUT(s): None // // INPUT(s): None // //================== Board::Board(){ // this is the default constructor for the class Board for( row = 0; row <= 7; row++ ){ for( col = 0; col <= 7; col++ ){ board[row][col] = 0; } // end of inner for } // end of outer for } // end of constructor //================== // OUTPUT(s): None // // INPUT(s): None // //=================== Board::~Board(){ // this is the de-constructor for the class Board } // end of de-constructor //============================================ // OUTPUT(s): Prints out the 2D chess board // // INPUT(s): None // //============================================ void Board::printBoard(){ // function header for the function printBoard of class Board; taking no arguments 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 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 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 } // end of inner for cout << "\n\n"; // prints out blank lines for spacing } // end of outer for } // end of member function printBoard for class Board //==================== // OUTPUT(s): ? // // INPUT(s): User inputs for the starting location of the knight // //==================== 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 bool pathFound = false; if( (row >= 0 && row <= 7) && (col >= 0 && col <= 7) ){ // if 1 if( board[row][col] == 0 ){ // if 2 board[row][col] = count; if( count == 64 ){ pathFound = true; } // end of if if(!pathFound){ // if 3 pathFound = knightTour( row - 2, col + 1, count + 1 ); if( !pathFound ){ pathFound = knightTour( row - 1, col + 2, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row + 1, col + 2, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row + 2, col + 1, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row + 2, col - 1, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row + 1, col - 2, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row - 1, col - 2, count + 1 ); } // end of if if( !pathFound ){ pathFound = knightTour( row - 2, col - 1, count + 1 ); } // end of if if( !pathFound ){ board[row][col] = 0; } // end of if } // end of if 3 } // end of inner if 2 } // end of outer if 1 return (pathFound); }; // end of member function knightTour for class Board
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
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 1
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)
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)
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.
.
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.
![]() |
Similar Threads
- trouble with knights tour (recursion) (C++)
- Recursivity (C++)
Other Threads in the C++ Forum
- Previous Thread: compressed file project
- Next Thread: how to create a Linux distro independent code
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






[/edit] 