0% found this document useful (0 votes)
687 views8 pages

8 Queens Problem

The document describes the eight queens puzzle and provides code templates for methods to solve it on an n x n chessboard. The puzzle aims to place 8 queens on an 8x8 board so that no two queens share the same row, column or diagonal. The code templates include methods to check the validity of a solution by checking rows, columns, and diagonals and printing the board. The document asks the reader to complete the code templates to find all solutions for boards of size 6x6, 7x7 and 8x8.

Uploaded by

naja man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
687 views8 pages

8 Queens Problem

The document describes the eight queens puzzle and provides code templates for methods to solve it on an n x n chessboard. The puzzle aims to place 8 queens on an 8x8 board so that no two queens share the same row, column or diagonal. The code templates include methods to check the validity of a solution by checking rows, columns, and diagonals and printing the board. The document asks the reader to complete the code templates to find all solutions for boards of size 6x6, 7x7 and 8x8.

Uploaded by

naja man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

505247510.

docx

The eight queens puzzle consists of putting eight chess


queens on an 8×8 chessboard such that none of them is
able to capture any other using the standard chess
queen's moves. Thus, a solution requires that no two
queens share the same row, column, or diagonal.

Here is almost a solution of the 8-queens problem


(Can you find the error?)

The problem was generalized to n x 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.

There are some practical applications to the queens puzzle, 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.

Your job is to complete 5 methods: check to see if a potential solution is valid, and print
out the grid in row major order.

See next page…


Your code should test to see if a valid solution is found by checking that there is only 1
queen in each row, column, up diagonal, and down diagonal on an n x n 2D-Array. (see
diagrams)

Please Note: In your code a 0 will indicate that a square is empty and a 1 will indiacte a
queen occupies the square.

You will be writing the 4 methods below which will return boolean - true or false.

Example:

rowCheck  should return true columnCheck  should return true

upDiagCheck  should return true downDiagCheck  should return false


(NOT A SOLUTION!)

You will also write the printGrid method which prints the grid as rows and columns.

See code on the next page:


/***
* Find solutions to the n queens problem.
*
* In your code a 0 will indicate that a square is
* empty and a 1 will indiacte a queen occupies the square.
*
* BELOW IS THE EXPECTED OUTPUT
*
*010000
*000100
*000001
*100000
*001000
*000010
* For a 6X6 grid there were 4 solutions total. Above is one of them.
*
*1000000
*0010000
*0000100
*0000001
*0100000
*0001000
*0000010
* For a 7X7 grid there were 40 solutions total. Above is one of them.
*
*10000000
*00001000
*00000001
*00000100
*00100000
*00000010
*01000000
*00010000
* For an 8X8 grid there were 92 solutions total. Above is one of them.
*
***/
public class EightQueen
{

/**
* 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 static boolean rowCheck(int[][] grid) {
// <<< 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 static boolean colCheck(int[][] grid) {
// <<< 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.
*
* This is a tricky method to write. It will require two nested loops.
*
* One of the nested loops will check all of the up diagonals starting
* from colum 0 and ending in row 0. (This loop has been written for you!)
*
* The second nested loop will check diagonals starting on the bottom row
* and ending in the last colums.
*/
public static boolean upDiagCheck(int[][] grid) {
/**
* checks the up diagonals starting from colum 0 and ending in row 0.
*/
int sum;
int col;
int row;
for (int start = 0; start < grid.length; start++) {
sum = 0;
row = start;
col = 0;
while (row>=0) {
sum += grid[row][col];
row--;
col++;
}
if (sum > 1) return false;
}
// <<< Complete the code for the second loop>>>

/**
* 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.
*
* This method is similar to upDiagCheck(). It also requires two
* nested loops. Use upDiagCheck() as a starting point.
*/
public static boolean downDiagCheck(int[][] grid) {
// <<< 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 static void printGrid(int[][] grid) {
// <<< Complete the code >>>
}

/** ------------ <<<<<< The main method is complete >>>>>>> ---------


* This method places 0's and 1's on the grid, and tests to
* determines if all rows, columns, diagonals contain only 1 queen.
*/
public static void main(String[] args)
{
int[][] g6 = new int[6][6];
int count = 0;
for (int a = 0; a<6; a++)
for (int b = 0; b<6; b++)
for (int c = 0; c<6; c++)
for (int d = 0; d<6; d++)
for (int e = 0; e<6; e++)
for (int f = 0; f<6; f++)
{
g6[0][a] = 1;
g6[1][b] = 1;
g6[2][c] = 1;
g6[3][d] = 1;
g6[4][e] = 1;
g6[5][f] = 1;
if (downDiagCheck(g6) && upDiagCheck(g6) && rowCheck(g6) && colCheck(g6) )
{
if (count==0)
printGrid(g6);
count++;
}
g6[0][a] = 0;
g6[1][b] = 0;
g6[2][c] = 0;
g6[3][d] = 0;
g6[4][e] = 0;
g6[5][f] = 0;
}
System.out.println("For a 6X6 grid there were " + count + " solutions total. Above is one of
them.");

int[][] g7 = new int[7][7];


count = 0;
for (int a = 0; a<7; a++)
for (int b = 0; b<7; b++)
for (int c = 0; c<7; c++)
for (int d = 0; d<7; d++)
for (int e = 0; e<7; e++)
for (int f = 0; f<7; f++)
for (int g = 0; g<7; g++)
{
g7[0][a] = 1;
g7[1][b] = 1;
g7[2][c] = 1;
g7[3][d] = 1;
g7[4][e] = 1;
g7[5][f] = 1;
g7[6][g] = 1;
if (downDiagCheck(g7) && upDiagCheck(g7) && rowCheck(g7) && colCheck(g7) )
{
if (count==0)
printGrid(g7);
count++;
}
g7[0][a] = 0;
g7[1][b] = 0;
g7[2][c] = 0;
g7[3][d] = 0;
g7[4][e] = 0;
g7[5][f] = 0;
g7[6][g] = 0;

}
System.out.println("For a 7X7 grid there were " + count + " solutions total. Above is one of
them.");

int[][] g8 = new int[8][8];


count = 0;
for (int a = 0; a<8; a++)
for (int b = 0; b<8; b++)
for (int c = 0; c<8; c++)
for (int d = 0; d<8; d++)
for (int e = 0; e<8; e++)
for (int f = 0; f<8; f++)
for (int g = 0; g<8; g++)
for (int h = 0; h<8; h++)
{
g8[0][a] = 1;
g8[1][b] = 1;
g8[2][c] = 1;
g8[3][d] = 1;
g8[4][e] = 1;
g8[5][f] = 1;
g8[6][g] = 1;
g8[7][h] = 1;
if (downDiagCheck(g8) && upDiagCheck(g8) && rowCheck(g8) && colCheck(g8) )
{
if (count==0)
printGrid(g8);
count++;
}
g8[0][a] = 0;
g8[1][b] = 0;
g8[2][c] = 0;
g8[3][d] = 0;
g8[4][e] = 0;
g8[5][f] = 0;
g8[6][g] = 0;
g8[7][h] = 0;
}
System.out.println("For an 8X8 grid there were " + count + " solutions total. Above is one of
them.");
}
}

You might also like