Question: I need help with the problem KNIGHTS TOUR. The code has to be in c++ so this is what i have so far. i am
I need help with the problem KNIGHTS TOUR. The code has to be in c++ so this is what i have so far. i am having problem in printing the output. please help i thing the problem is in my for loop.
#include
int route = 0; const int M = 8; bool findway(int R, int C, int index, int M); bool Move(int R, int C, int M); int solarray [8][8];
int main() { for (int I = 0; I < M; I++) { for (int X = 0; X < M; X++) { solarray[I][X] = 0; } } if (findway(0, 0, 0, sizeof(solarray)/sizeof(solarray[0]))) { for (int A = 0; A < sizeof(solarray); A++) { for (int B = 0; B < sizeof(solarray); B++) { cout < } } else { cout<<"Fail to find the path"<< endl; } return 0; } bool findway(int R, int C, int index, int M) { if (solarray[R][C] != 0) { return false; } solarray[R][C] = route++; if (index == M * M - 1) { return true; } if (Move(R + 2, C - 1, M) && findway(R + 2, C - 1, index + 1, M)) { return true; } if (Move(R + 1, C - 2, M) && findway(R + 1, C - 2, index + 1, M)) { return true; } if (Move(R - 1, C - 2, M) && findway(R - 1, C - 2, index + 1, M)) { return true; } if (Move(R - 2, C - 1, M) && findway(R - 2, C - 1, index + 1, M)) { return true; } if (Move(R - 2, C + 1, M) && findway(R - 2, C + 1, index + 1, M)) { return true; } if (Move(R + 2, C + 1, M) && findway(R + 2, C + 1, index + 1, M)) { return true; } if (Move(R - 1, C + 2, M) && findway(R - 1, C + 2, index + 1, M)) { return true; } if (Move(R + 1, C + 2, M) && findway(R + 1, C + 2, index + 1, M)) { return true; } solarray[R][C] = 0; route--; return false; } bool Move(int R, int C, int M) { if (R >= 0 && C >= 0 && R < M && C < M) { return true; } return false; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
