0

Currently i am trying to understand recursion. After implementing some easy recursive algorithms like factorial i decided to try something harder.

I found out about knight's tour problem and i tried to make a programme that finds a valid knight's tour.

Of course it doesn't work and output is an infinite loop.

I can't see the error. I'm learning by myself and dont have anyone to ask about this so i decided to ask here.

Here is my c++ code :

#include<iostream>
using namespace std;
#define N 8
int tab[N][N];
void fill_with_zeros();
void display_array();
bool is_move_correct(int nx,int ny);
bool try_moving(int x,int y,int &nx,int &ny,int option);
bool knights_tour(int x,int y,int move_number);

int main()
{
    fill_with_zeros();
    knights_tour(0,0,1); //Passing coordinates and number of moves
    return 0;
}



bool is_move_correct(int nx,int ny) //This functions checks if coordinates are valid
{
    if(nx<N && ny<N && nx>=0 && ny>=0 && tab[nx][ny] == 0) return true;

    return false;
}


bool try_moving(int x,int y,int &nx,int &ny,int option)
{
    switch(option)
    {
    case 1 :  { if(is_move_correct(x+2,y+1)==true)  { nx = x +2,ny = y+ 1;return true;}}

    case 2 :  { if(is_move_correct(x+2,y-1)==true)  { nx = x +2,ny = y- 1;return true;}}

    case 3 :  { if(is_move_correct(x-2,y+1)==true)  { nx = x -2,ny = y + 1;return true;}}

    case 4 :  { if(is_move_correct(x-2,y-1)==true)  { nx = x -2,ny = y - 1;return true;}}

    case 5 :  { if(is_move_correct(x+1,y+2)==true)  { nx = x +1,ny = y +2;return true;}}

    case 6 :  { if(is_move_correct(x+1,y-2)==true)  { nx = x +1,ny = y -2;return true;}}

    case 7 :  { if(is_move_correct(x-1,y+2)==true)  { nx = x -1,ny = y +2;return true;}}

    case 8 :   { if(is_move_correct(x-1,y-2)==true)  { nx = x -1,ny = y -2;return true;}}

    }

    return false;
}


bool knights_tour(int x,int y,int move_number)
{
    tab[x][y] = move_number;

    if(move_number == N*N-1)
    {
        cout<<"Here is the knight's path : "<<endl;
        display_array();
        return true;
    }

// ******************************************************************************
//PROBLEM MUST BE IN THIS PART OF FUNCTION BUT I'M NOT ABLE TO TELL WHERE EXACTLY 
    else
    {
        //cout<<"Current move number : "<<move_number<<endl;
        int nx = 0,ny = 0;

        for(int w = 1; w<=8; w++)
        {
            if (    try_moving(x,y,nx,ny,w) == true )
            {
                if( knights_tour(nx,ny,move_number+1) == true )
                {
                    return true;
                }
            }
        }
    tab[x][y] = 0;
    }
    return false;
}
// ******************************************************************************
// These functions don't need any commentary : 

void fill_with_zeros()
{
    for(int i =0; i<N; i++)
    {
        for(int j = 0; j<N; j++)
        {
            tab[i][j] = 0;
        }
    }
}

void display_array()
{
    for(int i =0; i<N; i++)
    {
        for(int j = 0; j<N; j++)
        {
            cout<<tab[i][j]<<" ";
        }
        cout<<endl;
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
  • 2
    Are you sure the output is infinite? Maybe you just need a few trillion years of patience. – Hans Klünder Dec 28 '14 at 23:34
  • 1
    @HansKlünder is probably right. After changing value of `N` from 8 to 4 it finds path http://coliru.stacked-crooked.com/a/7650387f08c609ff (and with 8 you can perform DOS attacks on coliru :/ ) – mip Dec 28 '14 at 23:38
  • ;/ Damn, i ll try to improve this programme. Hans is right i need a few trillion years of patience. Any suggestions will be appreciated. – Piotr Wilk Dec 28 '14 at 23:47

1 Answers1

1

I don't think this is infinite. However, you're running an exponential recursion function with O(8^N) iterations. This will take a VERY long time to finish - hours, or even years. You should refine your algorithm to see if you can do it in polynomial time. (Looks like @HansKlünder pointed out the same thing.)

Searching the web for "knight's tour algorithm" turns up a number of good algorithms you could try implementing. This question has an implementation in Java posted only a few days ago, and the author has offered to help out.

Community
  • 1
  • 1
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54