0

I have been working on this knight's tour recursion problem for days and still can't find a solution. Can someone help me identify the error? I've been searching for it for a long time and the maximum tiles it can fill up to is only 60. I have no idea what the error is. Here is my code:

#include <iostream>;
#include <string>;
#include <vector>;
#include <cstdlib>;
#include <cmath>;
#include <ctype.h>;
#include <ctime>;
#include <stdlib.h>;
using namespace std;
int step=1, v[8][8], x[8], y[8];
bool success;
bool SolveKnightTour(int step, int xc, int yc){
    if (step>64){
    success=true;
    }
    else {
    success=false;
        int move=0;
        while(move<8 & success==false){
            if (xc+x[move]>=0 & xc+x[move]<8 & yc+y[move]>=0 & yc+y[move]<8 & v[yc+y[move]][xc+x[move]]==0){
            v[yc+y[move]][xc+x[move]]=step+1;
                if (!SolveKnightTour(step+1, xc+x[move], yc+y[move])){
                v[yc+y[move]][xc+x[move]]=0;
                }
            }
            if (success==false){
            move++;
            }
        }
    }
    return success;
}
int main(){
    int i, j;
    x[0]=1, y[0]=-2;
    x[1]=2, y[1]=-1;
    x[2]=2, y[2]=1;
    x[3]=1, y[3]=2;
    x[4]=-1, y[4]=2;
    x[5]=-2, y[5]=1;
    x[6]=-2, y[6]=-1;
    x[7]=-1, y[7]=-2;
    for (i=0;i<8;i++){
        for (j=0;j<8;j++){
        v[i][j]=0;
        }
    }
    v[4][4]=1;
    SolveKnightTour(step, 4, 4);
    for (i=0;i<8;i++){
        for (j=0;j<8;j++){
        cout<<v[i][j]<<" ";
        }
        cout<<"\n";
    }

    system("pause");

    return 0;
}

Any help is greatly appreciated.

BAW
  • 1
  • 1
  • 2
    Related: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – UKMonkey Jul 30 '18 at 09:21
  • 1
    Random observations: 1) `;` after the `#include`s are unnecessary. 2) Inconsistent use of C headers -- e.g. `` but ``. 3) C headers, why? 4) C arrays, why? (Especially since you **do** `#include `...) 5) `SolveKnightTour()` advertised to return `bool` but return code not checked. -- And I prefer operators being whitespace-padded for legibility (`for ( j = 0; j < 8; ++j )` instead of `for (j=0;j<8;j++)`) and ANSI-style indentation, but that's a matter of taste. – DevSolar Jul 30 '18 at 09:28
  • So how do I fix the "return code not checked"? – BAW Jul 30 '18 at 09:43
  • `while(move<8 & success==false){` should be `while(move<8 && success==false){` but in this case I'm not sure it explains the problem – john Jul 30 '18 at 09:45
  • Same issue elsewhere `xc+x[move]>=0 & xc+x[move]<8` should be `xc+x[move]>=0 && xc+x[move]<8` etc. – john Jul 30 '18 at 09:47

0 Answers0