2

In the 8X8 chess board, taking only knight into consideration, if we start the knight from any square of the chessboard, the aim is to cover max number of square , without repeating any square . so far I have found the most efficient solution with my code below is:

60    29    34    49    0    15    46    0

35    50    1    16    45    48    11    0

30    59    28    33    2    9    14    47

51    36    31    44    17    12    3    10

58    43    52    27    32    25    8    13

37    40    55    18    23    6    21    4

42    57    38    53    26    19    24    7

39    54    41    56    0    22    5    20

Where the number starting with 1, shows the path followed by knight. My question is can this code be corrected for a perfect answer which is 64 (mine reaches only 60)?

package game;
import java.io.BufferedReader;  
import java.io.IOException;
import java.io.InputStreamReader;

public class Knight {
static int board[][]=new int[8][8];
static int value=1;
public static void zero()
{
    for(int i=0;i<7;i++)
        for(int j=0;j<7;j++)
            board[i][j]=0;
}

public static void knightpos(int x,int y)throws IOException
{
    if(value==61)
    {   System.out.println();
    for(int i=0;i<8;i++)
    {
        System.out.println();
        System.out.println();
        for(int j=0;j<8;j++)
        System.out.print("    "+board[i][j]);
    }

        System.exit(0);
    }


    if(x+1<=7&&y+2<=7)
    {
        if(board[x+1][y+2]==0)
        {  board[x+1][y+2]=value++;
           knightpos(x+1,y+2);
        }
    }

    if(x+2<=7&&y+1<=7)
    {
         if(board[x+2][y+1]==0)
        {
           board[x+2][y+1]=value++;
           knightpos(x+2,y+1);
        }
    }

    if(x-2>=0&&y-1>=0)
    {
        if(board[x-2][y-1]==0)
        {board[x-2][y-1]=value++;
           knightpos(x-2,y-1);
        }
    }

    if(x+2<=7&&y-1>=0)
    {
          if(board[x+2][y-1]==0)
        {board[x+2][y-1]=value++;
           knightpos(x+2,y-1);
        }
    }

    if(x+1<=7&&y-2>=0)
    {
        if(board[x+1][y-2]==0)
        {board[x+1][y-2]=value++;
           knightpos(x+1,y-2);}
    }

    if(x-1>=0&&y-2>=0)
    {
          if(board[x-1][y-2]==0)
        {board[x-1][y-2]=value++;
           knightpos(x-1,y-2);}
    }

    if(x-2>=0&&y+1<=7)
    {
          if(board[x-2][y+1]==0)
        {board[x-2][y+1]=value++;
           knightpos(x-2,y+1);}
    }

    if(x-1>=0&&y+2<=7)
    {
          if(board[x-1][y+2]==0)
        {board[x-1][y+2]=value++;
           knightpos(x-1,y+2);}
    }
    board[x][y]=0;
    value--;
    return;
}

public static boolean chk() {

    for(int i=0;i<7;i++)
        for(int j=0;j<7;j++)
            if(board[i][j]==0)
                return false;

    return true;

}


public static void main(String args[])throws IOException
{
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    System.out.println("Knight chess game input x,y position ");
    int x=Integer.parseInt(br.readLine());
    int y=Integer.parseInt(br.readLine());
{
    if(!chk())
            {   
                zero();
                value=1;
                knightpos(x,y);
            }

}           
}
}
false
  • 10,264
  • 13
  • 101
  • 209
Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52
  • 1
    Have you tried a profiler to analyse the bottlenecks? – Jorge Leitao May 15 '12 at 12:28
  • 4
    This belongs on http://codereview.stackexchange.com – Stephen C May 15 '12 at 12:29
  • profiler ??? @J.C.Leitão help me with this . – Ratan Kumar May 15 '12 at 12:33
  • If you are looking for resources concerning the Knight Tour problem, you can take a look on [Wikipedia](http://en.wikipedia.org/wiki/Knight's_tour) – Seki May 15 '12 at 12:35
  • There are some programs that already analyse the performance of your code. Of course they don't tell you how to solve the bottleneck, but they help you to find then. Search on wiki for it. – Jorge Leitao May 15 '12 at 12:36
  • 2
    You don't need to optimize, you need to find a better solution as yours is not complete. A profiler will just help on finding bad design and timing issues, but it won't help to find another algorithm. – Seki May 15 '12 at 12:38
  • 2
    This code is actually not working, so it does not belong on Code Review. – Bill the Lizard May 15 '12 at 12:40
  • @BilltheLizard i have successfully , executed my code in eclipse , and the solution i posted above is wat i got from my code execution . – Ratan Kumar May 15 '12 at 12:42
  • What happens if you change `if(value==61)` to `if(value==65)`? – Russell Zahniser May 15 '12 at 12:52
  • @RussellZahniser the execution goes into somewhat non termination mode. – Ratan Kumar May 15 '12 at 13:02
  • A fully executed knightstour should be able to start at any square implementing backtracking. if you reach a dead end you simply back down to the next square and try to move to a different square and you keep on backtracking until you find a correct movement sequence. Make sure this is not a requirement for your assignment – John Snow May 15 '12 at 13:26

3 Answers3

1
60    29    34    49    0    15    46    0

35    50    1    16    45    48    11    0

30    59    28    33    2    9    14    47

51    36    31    44    17    12    3    10

58    43    52    27    32    25    8    13

37    40    55    18    23    6    21    4

42    57    38    53    26    19    24    7

39    54    41    56    0    22    5    20

If you look at your solution, the first step, where you could branch to a zero is step 3. As additional information, we see, that you could have jumped from 60 to 1 again, building a cycle (but you couldn't, because you aren't allowed to visit a place twice.

But if you start at 4, you can move to 60, from there to 1, 2, 3 and now you can visit one of the zero fields.

However, that's only an improvement for 1 field. Since the other unvisited fields aren't visitable in sequence, this can't be improved further in the same way.

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • 1
    this wat i was exactly asking , does there exist any path which can travel this in perfect way to all 64 squares. anyway thanks for the extra one . – Ratan Kumar May 15 '12 at 13:05
  • @Ratan then your question was improperly phrased. You're not looking for an improvement to the _algorithm_. You're looking for a starting square which works with _this_ algorithm. Totally different, yes? – CPerkins May 15 '12 at 13:39
1

An old heuristic which works very well for knight's tour(even on paper) is always jump to the MOST restricted square, that is a place where the knight has the least moves.

If there are more than one square with same restrictions then choose one of them at random.

It has been proven that you could theoretically get a dead end with this rule but usually(I think it was more than 99%) it works out to full 64 tour.

Sint
  • 1,580
  • 3
  • 21
  • 38
  • most restricted square ???? how can this be achieved programatically ?? can you show us a path which has all 64 square followed. – Ratan Kumar May 15 '12 at 13:07
0

You make a method that returns a list of the possible cells for the given current cell the knight is in and then order them by the number of elements each of the cell would have as candidates, in ascending order.
The recursive call will try first those cells with a lower number of candidates, and get to solutions quicker. For the full set of solutions you still have to explore the whole search area, though.

Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
heyhey
  • 1