Knight's Tour Backtrack

Can you check this code? It's stuck in infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
using namespace std;

int xs[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int ys[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int b[12][12];

bool knighttour(int x, int y, int s)
{
    int c=0;
    bool done = false;
    do{
        if (b[x+xs[c]][y+ys[c]] == 0){
            b[x+xs[c]][y+ys[c]] = s;
            if (s<=64){
                done = knighttour(x+xs[c], y+ys[c], s+1);
                if (!done){
                    b[x+xs[c]][y+ys[c]] = 0;
                }
            }
            else{
                done = true;
            }
        }
        c+=1;
    }while (!done && c<8);
    return done;
}

int main()
{
    for (int i=0; i<12; i++){
        for (int j=0; j<12; j++){
            if (i>1 && i<10 && j>1 && j<10){b[j][i] = 0;}
            else {b[j][i] = 1;}
        }
    }

    int x, y;

    cout << "Initial Coordinate (x y): ";
    cin >> x >> y;
    x += 1; y += 1;
    b[x][y] = 1;

    knighttour(x, y, 2);

    for (int i=2; i<10; i++){
        for (int j=2; j<10; j++){
            if (b[j][i]<10){cout << " " << b[j][i] << " ";}
            else {cout << b[j][i] << " ";}
        }
        cout << endl;
    }

    return 0;
}
Topic archived. No new replies allowed.