knights tour algorithm

Hi, was hopin someone could point me in the right direction where i have gone wrong. The program compiles and takes the user input but doesnt seem to be able to find a complete tour.

All help is appreciated

//Solving the Knights tour Problem : Moving a knight on an otherwise

//empty chessboard until it has visited each square exactly once

import java.io.*;

class Graph{

// Adjacency matrix which represents chessboard

privatestaticint board [][];

privateint boardsize;

public Graph (int boardsize)//constructor

{

board =newint [boardsize][boardsize];

//Set adj matrix to 0

for (int i=0; i<boardsize; i++)

for (int j=0; j><boardsize; j++)

board [i][j] = 0;

}//end constructor

//Is proposed move on the board

privatestaticboolean onBoard(int row,int col){

return (row >=0 && row < boardsize && col>=0 && col < boardsize);

}

//Is square available to move to

privatestaticboolean available (int row,int col){

return (board[row][col]== 0);

}

//Main method - Does the searching for a solution from a given start point

privatestaticboolean doTour(int row,int col,int moveNum){

int nr=0, nc=0;

//Can the knight move to proposed square?

if (!onBoard(row, col) || !available(row, col))

returnfalse;

//Record move

board[row][col] = moveNum;

//Has a solution been found?

if(moveNum == boardsize*boardsize)

returntrue;

for (int i=0;i<8;i++)

{

switch (i)

{

case 0: nr= row-2; nc= col+1;

break;

case 1: nr = row-2; nc=col-1;

break;

case 2: nr = row +2; nc =col+1;

break;

case 3: nr = row+2; nc = col-1;

break;

case 4: nr = row+1; nc=col+2;

break;

case 5: nr = row+1; nc = col-2;

break;

case 6: nr = row-1; nc = col+2;

break;

case 7: nr = row-1; nc = col-2;

break;

}

if (doTour (nr,nc,moveNum+1))

returntrue;

}

//all eight possible moves tried no success

//unrecord move and backtrack

board[row][col] = 0;

returnfalse;

}

publicstaticvoid main(String [] args)

{

InputStreamReader stdin =new InputStreamReader(System.in);

BufferedReader console =new BufferedReader(stdin);

int initX = 0,initY = 0, boardsize = 0;//

String s1,s2, s3;

try

{

System.out.print("Enter starting row ");

s1 = console.readLine(); initX = Integer.parseInt(s1);

System.out.print("Enter starting column ");

s2 = console.readLine(); initY = Integer.parseInt(s2);

System.out.print("Enter boardsize");

s3 = console.readLine(); boardsize = Integer.parseInt(s3);

}

catch(IOException ioex)

{

System.out.println("Input error");

System.exit(1);

}

catch(NumberFormatException nfex)

{

System.out.println("\"" + nfex.getMessage() +"\" is not numeric");

System.exit(1);

}

Graph theGraph =new Graph(boardsize);

//Begin tour

if (doTour(initX, initY,1))

{ System.out.println ("tour successful contents are: ");

for (int i=0; i < boardsize;i++)

{

for (int j=0;j<boardsize;j++)

System.out.print(board[i][j]+"**");

System.out.println();

}

}

else System.out.println ("Tour not possible from (" + initX +"," + initY +")");

}

}

[>

[7658 byte] By [vuriela] at [2007-11-27 4:37:08]
# 1
You don't expect us to debug and write your program do you, because thats the only way to solve the problem.Try searching the forum. I've seen questions on this topic before (either in this forum or the Swing forum). Then you can compare the code in the other posting with yours.
camickra at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 2
it really compiles? statics and non-statics and all?
petes1234a at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 3

Once you get it compiling, one hint. At the top of your doTour routine, put in a System.out.println line to output row and col. It will be enlightening:

private boolean doTour(int row, int col, int moveNum) {

System.out.println("(" + row + ", " + col + ")"); // add this row

.....................

.....................

Good luck,

/Pete

petes1234a at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 4

Yeah it does compile but it obviously isnt correct. I think it has to do with my boardsize

variable. If i hard code it so that it has a value to begin with, ie private static int boardsize= 7

, this will actually run and output a correct tour, but what im trying to do is have the user input the boardsize to be used.

I have had a look at the other forum threads on this matter but none are applcable to this problem an its driving me crazy not understanding what it is!

Message was edited by:

vuriel

vuriela at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 5
Having the user input the board size is nice, but you should perform some validation on the input. First of all, are you absolutely certain that all the values are valid? I know for sure that every board size smaller than 3 is not solvable.
Dalzhima at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 6
Yeah sorry i should have said. I have read also that smaller sized boards do not allow a complete tour, so the minimum dimensions would be a 5x5 board.
vuriela at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 7

The "classic" board size is of course 8x8. But regardless, I still recommend that you have a peek at the row and col variable values as they are created. Something is amiss when I tried your prog with an 8x8 grid. If you add my suggested code, you will see the row and col values flying by, and will see what I mean.

petes1234a at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 8

Sorry pete, i added your code an realised i had made a mistake in the available method. It should read //Is square available to move to

private static boolean available (int row, int col) {

return (board[row][col]== -1);

}

and not == 0 as i have posted earlier. This allows tours to be made if the value of boardsize is hard coded. Im still working on how to define the boardsize by user input, If anyone has some thoughts on it i would be grateful.

Message was edited by:

vuriel

vuriela at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 9
Hey, just to say i finally figured it out, was quite simple in the end, i guess working on it late at night doesnt help!Thanks for the comments Vuriel
vuriela at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...
# 10
I am searching for linear algorithm code for that problem? Could you help me?
mromana at 2007-7-12 9:47:20 > top of Java-index,Java Essentials,Java Programming...