1

This code is not giving any output. It should output a matrix of 8X8 size.

#include <iostream>
#define N 8
using namespace std;

This function prints the matrix:

void print(int arr[][N]){
    int i, j;
    for (i = 0; i < N; i++){
        for (j = 0; j < N; j++)
            cout<<arr[i][j]<<" ";
    cout<<endl;
    }
}

This function checks limits of x and y and whether knight already visited that place or not.

bool safe(int x, int y, int arr[][N]){
    if(x>=0 && y>=0 && x<N && y<N && arr[x][y] == 0){
        return true;
    }
    return false;
}

This function do most of the things:

bool solveKT(int x, int y, int movei, int arr[][N], int xmove[], int ymove[] ){
    if(movei == (N*N)+1){
        return true;
    }

    for(int k=0 ; k<8 ; k++){
        int nextx = x + xmove[k];
        int nexty = y + ymove[k];

        if(safe(nextx, nexty, arr) == true){
            arr[nextx][nexty] = movei;
            if(solveKT(nextx, nexty, movei+1, arr, xmove, ymove) == true){
                return true;
            }
            arr[nextx][nexty] = 0; // backtrack
        }
    }
    return false;
}

This is just a driver function:

int main(){

    int arr[N][N]={0};
    int xmove[] = {1, 1,2, 2,-1,-1,-2,-2};
    int ymove[] = {2,-2,1,-1, 2,-2, 1,-1};
    arr[0][0] = 1;
    int movei = 2;
    bool a = solveKT(0, 0, movei, arr, xmove, ymove);
    if(a == true){
        print(arr);
    }
    else
        cout<<"no solution";

}
false
  • 10,264
  • 13
  • 101
  • 209
  • Please add a language tag and remove the language from the title, and more importantly write a more descriptive title – user12205 Jun 16 '15 at 09:35
  • Sure, the debugger can. Learn to compile with all warnings & debug info (`g++ -Wall -Wextra -g`) and **use the debugger** (`gdb`) - e.g. to run your program step by step. – Basile Starynkevitch Jun 16 '15 at 13:33
  • 1
    How about adding some tracing output showing you what the program does and what it does not? Come on, basic debugging isn't that hard... – DevSolar Jun 16 '15 at 13:33
  • 3
    **Wait**... are you really trying to **brute-force** a solution for the Knight's Tour? [4x10^51 possible move sequences](http://www.josiahland.com/archives/781)? When do you expect the solution to print? Within your life span? – DevSolar Jun 16 '15 at 13:39
  • yes know its not hard to debug. but the thing is i am using recursion and 8X8 so adding some tracing output is becoming difficult and i am not that experienced in programming. – anirudh goel Jun 16 '15 at 13:40
  • its not brute-force.. using backtracking. this program will not consider all the configuration. here is the link of what i am trying to do. http://www.geeksforgeeks.org/backtracking-set-1-the-knights-tour-problem/ – anirudh goel Jun 16 '15 at 13:41
  • 1
    How is going through all possible moves until you find a valid sequence *not* brute-forcing? Your `xmove` array has the first move not going to the right at index `[4]`... – DevSolar Jun 16 '15 at 14:04
  • 1
    Every time I see c and c++ tags in the same question i swear i will not look at this crap ever again. Yet, I'm still reading this... – zubergu Jun 16 '15 at 14:10
  • @zubergu: To give the OP credit, in this case the issue actually *is* topical for both C and C++. Aside from him using `cout` instead of `printf()`, users from both tags would be able to help him out -- and that's the purpose of those tags, isn't it? – DevSolar Jun 16 '15 at 14:15
  • @DevSolar: Pruning as soon as a path self-intersects and backtracking is a LOT more efficient than brute force enumeration of all possible sequences of 64 knight moves. – Ben Voigt Jun 16 '15 at 14:38
  • @BenVoigt: Still unoptimized as heck, and his problem is simply that the program takes ages to complete. – DevSolar Jun 16 '15 at 15:18
  • I guess the problem is that the solution is unoptimized and time of excution changes with reordering of xmove and ymove. It gives best result when xmove and ymove has 2 and 1 respectively as their first value... Thanks everyone...!!! – anirudh goel Jun 17 '15 at 11:50

1 Answers1

1

Replacing the following code:

if(movei == (N*N)+1){
    return true;
}

...with a hardcoded value...

if(movei == 62){
    return true;
}

...gave me a good result after 0.1 seconds. (A field with only three "zeroes" remaining.) So your overall algorithm works.

Hint for better looks of the output:

#include <iomanip>

cout << setw(3) << arr[i][j];

Bumping the 62 to 63 increased the runtime to 2.5 seconds, with only two zeroes on the field. Still waiting for the 64 run to finish.

Edit: Half an hour later, the 64 run still didn't finish. Point made.

Your problem is not the program, your problem is the algorithm. It has to go through trillions of moves to get a result. The complexity is killing you, as I guessed.

DevSolar
  • 67,862
  • 21
  • 134
  • 209