I was trying to implement a solution for knight tour problem. I faced an interesting issue. I would like to know the reason for the behavior.
This is the code - it works fine.
public class OpenKnightTour {
private int[][] chessTable;
private int startRow;
private int startCol;
private int ChessBoardSize;
private final int[][] moves = {
{-1, -2},
{-2, -1},
{-2, 1},
{-1, 2},
{1, -2},
{2, -1},
{2, 1},
{1, 2}
};
public OpenKnightTour(int ChessBoardSize, int startRow, int startCol){
this.ChessBoardSize = ChessBoardSize;
this.startRow = startRow;
this.startCol = startCol;
this.chessTable = new int[ChessBoardSize][ChessBoardSize];
}
void move(){
//first move
this.chessTable[this.startRow][this.startCol] = 1;
//start with 2nd move recursively
moveHelper(this.startRow, this.startCol, 2);
//print results
this.print();
}
private boolean moveHelper(int r, int c, int count){
if((count-1) == (this.ChessBoardSize * this.ChessBoardSize))
return true;
for(int[] move: moves){
int nextR = r + move[0];
int nextC = c + move[1];
if(isMoveValid(nextR, nextC)){
chessTable[nextR][nextC] = count;
if(moveHelper(nextR, nextC, count + 1)){
return true;
}
chessTable[nextR][nextC] = 0;
}
}
return false;
}
private void print(){
Arrays.stream(this.chessTable)
.forEach(a -> System.out.println(Arrays.toString(a)));
}
private boolean isMoveValid(int r, int c){
if(!(r >= 0 && r < this.ChessBoardSize))
return false;
if(!(c >= 0 && c < this.ChessBoardSize))
return false;
return chessTable[r][c]==0;
}
}
Now we could run this, starting with knight position as (2, 2)
in the matrix
OpenKnightTour o = new OpenKnightTour(8, 2, 2);
o.move();
When i tried to run the starting position as (0, 0)
, it ran continuously without showing any results. So i thought there could not be any solution for it. But if i change below order slightly, It works fine immediately.
private final int[][] moves = {
{-1, -2},
{-2, -1},
{-2, 1},
{-1, 2},
{1, -2},
{1, 2}, //moved from last
{2, -1},
{2, 1},
};
What is really going on? how does this position of moves could affect the performance this much? then in that case, how do we know the correct order of the moves?