-1

I am new to programming and i want resolve Knight's Tour as practice. But I don't understand where is the error on my program Here is my method:

 class Knight
{
    int N, M;
    bool[,] board ;

    public Knight()
    {
        N = 5;
        board = new bool[N, N];
        N *= N;
        M = N - 1;
    }



    public bool Movement(int y, int x, int mov)
    {

        if (x < 0 || x >= 5 || y < 0 || y >= 5) 
        {
            return false;
        }
        if (board[x, y] == true) // has been in that position
        {
            return false ;
        }
        if (mov == M)
        {
            Console.WriteLine("Solution");
            board[x, y] = true;
            return true;
        }
        else
        {

           bool result = false;

             result = result || Movement( x+2, y+1, mov+1);
             result = result || Movement( x+2, y-1, mov+1);
             result = result || Movement( x-2, y+1, mov+1);
             result = result || Movement( x-2, y-1, mov+1);
             result = result || Movement( x+1, y+2, mov+1);
             result = result || Movement( x+1, y-2, mov+1);
             result = result || Movement( x-1, y+2, mov+1);
             result = result || Movement( x-1, y-2, mov+1);

             if (result == true)
             {
                 Console.WriteLine("{0} {1}", x, y);

                 return true;
             }
             else
             {

                     board[x, y] = false;
                     return false;

             }
        }
    }

I do not know why I'm getting repeated coordinates here is the output:

2 1
3 1
3 4
2 4
2 1
3 1
3 4
2 4
2 1
3 1
3 4
2 4
2 1
3 1
3 4
2 4
2 1
3 1
3 4
2 2
4 1
3 3
1 2
0 0
false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

0

I think you are not setting

board[x, y] = true;

in your else case, in case

if (result == true)
S.Spieker
  • 7,005
  • 8
  • 44
  • 50