Almost a solution

advertisement
8 Queens Problem
The problem is to find all ways of placing n non-taking
queens on a n by n board. A queen attacks all cells in
its same row, column, and either diagonal. Therefore,
the objective is to place n queens on an n by n board in
such a way that no two queens are on the same row,
column or diagonal. Below we show a solution on a
standard 8 by 8 chessboard.
Almost a solution of the 8-queens problem
(Can you find the error?)
The n-queens problem is an old one that has been around for more than a century.
Originally known as the 8-Queens problem, it has been studied by many famous
mathematicians over the years, including the great German mathematician Karl Friedrich
Gauss (1777-1855). The problem was generalized to n by n boards in 1850 by Franz
Nauck. Since the 1960's, with rapid developments in computer science, this problem has
been used as an example of backtracking algorithms, permutation generation, the divide
and conquer paradigm, program development methodology, constraint satisfaction
problems, integer programming, and specification.
The queens problem is really a puzzle but, surprisingly, there are some practical
applications such as parallel memory storage schemes, VLSI testing, traffic control, and
deadlock prevention. The problem is also illustrative of more practical problems whose
solutions are permutations, of which there are many. One such problem is the travelling
salesperson problem: Find the shortest route on a map that visits each city exactly once
and returns to the starting city. Here the permutation is the list of cities that are visited,
except for the first.
We are going to use good old bruit force to solve the problem. There are 92 solutions,
your code should pront them all.
Your code should place the 8 queens on the grid and test
to see if it is a valid solution by checking that there is
only 1 queen in each rows, columns, up diagonals, and
down diagonals. (see diagrams)
row check
column check
up diagonals check
down diagonals check
(NOT A SOLUTION!)
If it is a valid solution you code should print the grid. Keep testing until you find and
print all 92 solutions.
/***
* Find all solutions to the n queens problem.
***/
public class EightQueen
{
/***
* The grid will initially be filled with all 0's
* eight 1's will be placed on the grid, representing the 8 queens
*/
private int[][] grid;
public EightQueen(int[][] g) {
grid = g;
}
/**
* returns true if each row on the grid contains at most one '1'
* else returns false
* This method should be written so that it would work on any size square grid.
*/
public boolean rowCheck() {
// <<< Complete the code >>>
}
/**
* returns true if each column on the grid contains at most one '1'
* else returns false
* This method should be written so that it would work on any size square grid.
*/
public boolean colCheck() {
// <<< Complete the code >>>
}
/**
* returns true if each "up" diagonal contains at most one '1'
* else returns false
* an "up" diagonal goes from lower left to upper right
* This method should be written so that it would work on any size square grid.
*/
public boolean upDiagCheck() {
// <<< Complete the code >>>
}
/**
* returns true if each "down" diagonal contains at most one '1'
* else returns false
* a "down" diagonal goes from upper left to lower right
* This method should be written so that it would work on any size square grid.
*/
public boolean downDiagCheck() {
// <<< Complete the code >>>
}
/**
* This method places 8-queens on the grid, and tests to
* determines if all rows, columns, diagonals contain only 1 queen.
* The grid is printed only if it is a valid solution.
* Every solution should be printed (92 solutions).
*/
public void check() {
// <<< Complete the code >>>
}
/***
* prints the grid - as rows and columns
* This method should be written so that it would work on any size square grid.
*/
public void printGrid() {
// <<< Complete the code >>>
}
/**
* This method is complete
*/
public static void main(String[] args) {
EightQueen q = new EightQueen(new int[][] {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}});
q.check();
}
}
Download