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:
Project 1 Due Date Problem Knight's Tour: The Knights Tour is a mathematical problem involving a...
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...
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 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 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...