Question

Project 1 Due Date Problem Knights Tour: The Knights Tour is a mathematical problem involving a knight on a chessboard. The knight is placed on the empty board and, moving acording to the rules of chess, must visit each square once. 06-JUN-2018 There are several billion solutions to the problem, of which about 122,000,000 have the knight finishing on the same square on which it begins. When this occurs the tour is said to be closed. Your assignment is to write a program that gives a solution to the Knights Tour problem recursively. You must hand in a solution in C++ AND Java. The name of the C++ file should be main.ce and the name of the Java file should be Main,java.Output should look similar to: 1 34 3 18 49 32 13 16 4 19 56 33 14 17 50 31 57 2 35 48 5S 52 15 12 0 5 60 53 36 47 30 51 58 37 46 61 54 11 26 6 21 42 59 38 27 64 29 43 40 23 45 62 25 10 22 7 44 39 24 9 28 63 41

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.java

import java.awt.Point;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main
{
int[][] chessBoard;

List<Point>[][] availableMoves;

final static int NOTUSED = -1;

final static Point[] DIRECTION = new Point[] { new Point( -1, -2 ), new Point( 1, -2 ), new Point( 2, -1 ), new Point( 2, 1 ), new Point( 1, 2 ),
new Point( -1, 2 ), new Point( -2, 1 ), new Point( -2, -1 ) };

int size;

public Main( int n )
{
size = n;
chessBoard = new int[n][n];
availableMoves = new List[n][n];
for ( int y = 0; y < n; y++ )
{
for ( int x = 0; x < n; x++ )
{
chessBoard[x][y] = NOTUSED;
availableMoves[x][y] = new ArrayList<>();
for ( Point d : DIRECTION )
{
//calculate the new position
int nx = x + d.x;
int ny = y + d.y;

//only process valid moves
if ( ( nx >= 0 && nx < size ) && ( ny >= 0 && ny < size ) )
{
availableMoves[x][y].add( new Point(nx,ny) );
}
}
}
}
//we need to sort the possiblemoves, so that the first move is the one that has the least possiblemoves
processedPossibleMoves( );
}


private void processedPossibleMoves()
{
for ( int y = 0; y < size; y++ )
{
for ( int x = 0; x < size; x++ )
{
List<Point> moves = availableMoves[x][y];

moves.sort( new Comparator<Point>()
{

@Override
public int compare( Point o1, Point o2 )
{
int o1Moves = availableMoves[o1.x][o1.y].size();
int o2Moves = availableMoves[o2.x][o2.y].size();
return o1Moves - o2Moves;
}
});
}
}
}

private void printBoard()
{
for ( int y = 0; y < size; y++ )
{
for ( int x = 0; x < size; x++ )
{
System.out.printf( "%2d ", chessBoard[x][y] );
}
System.out.println();
}
System.out.println();

}

private Main solve()
{
int x = 0;
int y = 0;
int current = 0;

discoverTour( x, y, current );
return this;
}

private boolean discoverTour( int x, int y, int current )
{
if ( chessBoard[x][y] != NOTUSED )
{
return false;
}

//Mark current positoin as 'current'
chessBoard[x][y] = current;

if ( current == size * size - 1 )
{
//done :)
return true;
}

for ( Point d : availableMoves[x][y])
{
if ( discoverTour( d.x, d.y, current + 1 ) )
{
return true;
}
}

//if we are here, all our options ran out :(
//reset the current cell and return false
chessBoard[x][y] = NOTUSED;

return false;
}

public static void main( String[] args )
{
new Main( 8 ).solve().printBoard();
}
}

SAMPLE OUTPUT:

CAWindows System321cmd.exe C:\UsersISAM\Desktoplcg javal1l>javac Main.java ote: Main.java uses unchecked or unsafe operations

Add a comment
Know the answer?
Add Answer to:
Project 1 Due Date Problem Knight's Tour: The Knights Tour is a mathematical problem involving a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • For this project, you are tasked with creating a text-based, basic String processing program that performs...

    For this project, you are tasked with creating a text-based, basic String processing program that performs basic search on a block of text inputted into your program from an external .txt file. After greeting your end user for a program description, your program should prompt the end user to enter a .txt input filename from which to read the block of text to analyze. Then, prompt the end user for a search String. Next, prompt the end user for the...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT