| | |
trouble with knights tour (recursion)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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.
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".
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....:(
•
•
Join Date: Aug 2009
Posts: 23
Reputation:
Solved Threads: 0
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; struct Square { int Row, IColumn; char Column; }Source, Target; int count1; bool Visited[9][9]; vector<int> MinNumberOfKnightMoves; int NRow[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int NColumn[] = {-1, 1, -2, 2, -2, 2, -1, 1}; bool isValid(int Row, int Column) { return (Row > 0 && Row < 9 && Column > 0 && Column < 9); } void KnightMoves(int NumberOfMoves, int Row, int Column) { if(NumberOfMoves==64) return; if(Row == Target.Row && Column == Target.IColumn) { MinNumberOfKnightMoves.push_back(NumberOfMoves); return; } for(int i = 0; i < 8; i ++) { if(isValid( Row + NRow[i], Column + NColumn[i]) && !Visited[ Row + NRow[i]][Column + NColumn[i]]) { Visited[Row][Column] = true; KnightMoves(NumberOfMoves + 1, Row + NRow[i], Column + NColumn[i]); Visited[Row][Column] = false; } } } int main() { string square; while(cin >> square) { count1 =0; Source.Column = square[0]; Source.IColumn = square[0] - 'a' + 1; Source.Row = square[1] - '0'; cin >> square; Target.Column = square[0]; Target.IColumn = square[0] - 'a' + 1; Target.Row = square[1] - '0'; for(int i = 0; i < 9; i ++) for(int j = 0; j < 9; j ++) Visited[i][j] = false; KnightMoves(0, Source.Row, Source.IColumn); sort(MinNumberOfKnightMoves.begin(), MinNumberOfKnightMoves.end()); cout << "To get from " << Source.Column << Source.Row << " to " << Target.Column << Target.Row <<" takes " << MinNumberOfKnightMoves[0] << " knight moves." << endl; MinNumberOfKnightMoves.clear(); } return 0; }
![]() |
Similar Threads
- Hotmail Access Trouble (Web Browsers)
- Recursivity (C++)
- Binary Search Tree (C++)
- Aurora Trouble... please help I cant delet it (Viruses, Spyware and other Nasties)
- Trouble opening certain files... (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Recursive Parse Traversal!
- Next Thread: classes & object
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





