patternjavaMinor
Knights Tour -- Something I am doing wrong
Viewed 0 times
toursomethingdoingwrongknights
Problem
I have an assignment to write a knights tour backtracking program in Java where the professor says to:
Eliminate backtracking time by:
I have done that with and attempted some other optimizations but my algorithm is still pretty slow for most squares. I went online and found my algorithm was pretty similar to others. I even took the algorithm from here and found it ran slow on the same spaces as mine (ahem pretty much never finishes) I was ready to call it until I took my processor's version from his website and it ran instantaneously on the squares every other version I had seen had run slowly on, he even showed me his code in his office last week and I recall it looking very similar. I have also seen questions on this site asking about their slow backtracking programs for knights tour. How is this possible!? Virtually every other source I have seen can confirm that this algorithm is slow except for my professor. My code:
```
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//Grid.java THIS IS WHERE THE MEAT AND POATATOS IS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
public class Grid
{
//Constants to keep things consistant and avoid errors.//
public final static char OBSTICLE_TILE_SYMBOLE = '0';
public final static char OPEN_TILE_SYMBOLE = '1';
private Tile[][] tileGrid;
/*****
'Makes for a nice api to do things this way,
no construtor discourages the user from *
** makin
Eliminate backtracking time by:
Find the knights with the least possible moves
Sort those moves from knight with least moves to greatest moves
Backtrack if that path failsI have done that with and attempted some other optimizations but my algorithm is still pretty slow for most squares. I went online and found my algorithm was pretty similar to others. I even took the algorithm from here and found it ran slow on the same spaces as mine (ahem pretty much never finishes) I was ready to call it until I took my processor's version from his website and it ran instantaneously on the squares every other version I had seen had run slowly on, he even showed me his code in his office last week and I recall it looking very similar. I have also seen questions on this site asking about their slow backtracking programs for knights tour. How is this possible!? Virtually every other source I have seen can confirm that this algorithm is slow except for my professor. My code:
```
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//Grid.java THIS IS WHERE THE MEAT AND POATATOS IS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
public class Grid
{
//Constants to keep things consistant and avoid errors.//
public final static char OBSTICLE_TILE_SYMBOLE = '0';
public final static char OPEN_TILE_SYMBOLE = '1';
private Tile[][] tileGrid;
/*****
'Makes for a nice api to do things this way,
no construtor discourages the user from *
** makin
Solution
Broken sort
When inserting elements into your list in sorted order, you don't insert the element at the correct index:
Suppose you had 10 elements already sorted and your new element were larger than index 4 but smaller than index 5. Your new element should be inserted at index 5 and shift all the other elements down. In your code, however, you are inserting at index 4, which is incorrect. You also have a check for
When inserting elements into your list in sorted order, you don't insert the element at the correct index:
for( int i = 0; i 0 )
{
tilesToTry.add( ( i - 1 ), currentHopTile );
tilesToTryLengths.add( ( i - 1 ), Integer.valueOf( CURRENT_LENGTH ) );
foundAPlace = true;
break;
}
}
}Suppose you had 10 elements already sorted and your new element were larger than index 4 but smaller than index 5. Your new element should be inserted at index 5 and shift all the other elements down. In your code, however, you are inserting at index 4, which is incorrect. You also have a check for
i > 0 which is wrong. Your code should be changed to this:for (int i = 0; i < AMOUNT_OF_TILES_TO_TRY; ++i)
{
if (CURRENT_LENGTH < tilesToTryLengths.get(i).intValue())
{
tilesToTry.add(i, currentHopTile);
tilesToTryLengths.add(i, Integer.valueOf(CURRENT_LENGTH));
foundAPlace = true;
break;
}
}Code Snippets
for( int i = 0; i < AMOUNT_OF_TILES_TO_TRY; ++i )
{
if( CURRENT_LENGTH < tilesToTryLengths.get( i ).intValue() )
{
if( i > 0 )
{
tilesToTry.add( ( i - 1 ), currentHopTile );
tilesToTryLengths.add( ( i - 1 ), Integer.valueOf( CURRENT_LENGTH ) );
foundAPlace = true;
break;
}
}
}for (int i = 0; i < AMOUNT_OF_TILES_TO_TRY; ++i)
{
if (CURRENT_LENGTH < tilesToTryLengths.get(i).intValue())
{
tilesToTry.add(i, currentHopTile);
tilesToTryLengths.add(i, Integer.valueOf(CURRENT_LENGTH));
foundAPlace = true;
break;
}
}Context
StackExchange Code Review Q#107923, answer score: 4
Revisions (0)
No revisions yet.