Super Queen Puzzle array exception - java

I'm trying to implement the N*N queen algorithm with a little twist to it. In this version the queen can also move like a Knight can in chess.
Checking for the diagonals and rows seems to be fine, however when I try to check for a queen in the "L" position away from my current position, I get a "arrayindexoutofboundexception".
I'm not entirely sure if my check_Knight move is right. I can't seem to locate the error causing the issue in my code.
public class QueenGame {
/**
* #param args
*/
static int solution =0;
static boolean check_Queen(int row, int col, int queens[])
{
for(int i =1; i<col; i++)
{
if (queens[col-i] == row ||
queens[col-i] == row-i ||
queens[col-i] == row+i)
{
//flag = false;
return false;
}
}
return true;
}
static boolean check_KnightMove(int row, int col, int queens[])
{
if(queens[col-2] == (row -1) || queens[col-2] == (row+1))
{
return false;
}
return true;
}
static void placement(int col, int queens[], int n){
//int solution =0;
for (int row = 1; row <= n; row++) {
queens[col] = row;
if((check_Queen(row,col,queens)) == true)
{
if((check_KnightMove(row,col,queens)) == true)
{
if(col == n)
{
solution++;
}
else
{
placement(col+1,queens,n);
}
}
}
}
queens[col] = 0;
}
public static void main(String[] args) {
int solution =0;
Scanner scanner=new Scanner(System.in);
//System.out.print("Please enter N");
int n =10;
//int n = scanner.nextInt();// TODO Auto-generated method stub
//System.out.print(n);
int queens[] = new int[n+1];
placement(1,queens,n);
System.out.println("nQueens: solution=" + solution);
}
}

If col is 0 or 1, then you will get an ArrayIndexOutOfBoundsException. Add a check before accessing the array.
if ((col >= 2) && (queens[col-2] == (row-1) || queens[col-2] == (row+1)))

Related

percolation problem (java -cannot run appropriately)

I have a problem relating to the percolation model with the latest version of Java
My project includes three file a illegalargumentexception.java file (use to handle illegal arguments), a percolation.java file (the datatype), and the PercolationStats.java file (use to calculate and generate the statistic)
In my expectation, the code should generate the probability of the percolated model (after a specific time of trials)
And here is my code:
the percolation datatype:
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class percolation {
public static boolean[] grid;
public static WeightedQuickUnionUF gridMap ;
public static int openCell;
public static int top = 0;
private static int current_index;
public int bottom;
private int n;
public percolation(int n) {
}
// creates n-by-n grid, with all sites initially blocked
public int Percolation (int n) {
gridMap = new WeightedQuickUnionUF(n*n + 2);
grid = new boolean[n*n + 2];
this.n =n;
openCell = 0;
top = 0;
bottom = n*n +1;
if (n <=0) {
throw new IllegalArgumentException("Grid's number can not negative or equal to zero");
}
return n ;
}
// opens the site (row, col) if it is not open already
public void open( int row, int col) {
current_index = (row-1)*n + (col-1);
grid[percolation.current_index] = true;
percolation.openCell++;
if (row == 1)
gridMap.union(percolation.current_index, top);
if(row == this.n)
gridMap.union(current_index, col);
if (row > this.n || col > this.n)
throw new IllegalArgumentException("Out of bound");
if (row <1 || col <1)
throw new IllegalArgumentException("The index must be greater than zero");
if(row >1 && isOpen(row-1, col)) // above cell
{
assert(current_index > n);
gridMap.union(current_index,current_index-n);
}
if (row < this.n && isOpen(row + 1 , col)) // below cell
{
assert(current_index + n < n * n);
gridMap.union(current_index , current_index + n);
}
if(col > 1 && isOpen(row, col - 1)) //left cell
{
gridMap.union(current_index,current_index- 1);
}
if(col < this.n && isOpen(row, col + 1)) //right cell
{
gridMap.union(current_index,current_index + 1);
}
return ;
}
// is the site (row, col) open?
public boolean isOpen(int row, int col) {
return grid[current_index];
}
// is the site (row, col) full?
public boolean isFull(int row, int col)
{
if(!isOpen(row,col)) return false;
return gridMap.find(current_index) == gridMap.find(top);
}
public int numberOfOpenSites() // returns the number of open sites
{
return percolation.openCell;
}
// does the system percolate?
public boolean percolates() {
return gridMap.find(top) == gridMap.find(bottom);
}
// test client (optional)
public static void main(String[] args) {
}
}
the PercolationStats.java file
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
public class PercolationStats {
public static double[] result;
public static int trials;
// perform independent trials on an n-by-n grid
public PercolationStats(int n, int trials) {
double[] result = new double[trials];
int test_row ;
int test_col ;
for (int i=0; i <trials; i++) {
percolation per = new percolation(n);
while (!per.percolates()) {
test_row = (int)((StdRandom.uniformInt(n)*n) +1 );
test_col = (int)((StdRandom.uniformInt(n)*n) +1 );
if( !per.isOpen(test_row,test_col) )
per.open(test_row,test_col);
}
result[i] = (double)(per.numberOfOpenSites()) / (n * n);
}
if(n <= 0 || trials <= 0) throw new IllegalArgumentException("Must greater than zero");
}
// sample mean of percolation threshold
public double mean() {
return StdStats.mean(result);
}
// sample standard deviation of percolation threshold
public double stddev() {
return StdStats.stddev(result) ;
}
// low endpoint of 95% confidence interval
public double Low_confidence() {
return (mean() - (1.96 * stddev())/ Math.sqrt(trials));
}
// high endpoint of 95% confidence interval
public double High_confidenceHi() {
return mean() + (1.96 * stddev())/ Math.sqrt(trials);
}
// test client (see below)
public static void main(String[] args) {
PercolationStats s = new PercolationStats(50,10000);
System.out.println("Mean:" + s.mean());
System.out.println("Standard Deviation:"+ s.stddev());
System.out.println("High Confidence" + s.High_confidenceHi());
System.out.println("Low Confidence" + s.Low_confidence());
}
}
My expectation is when I compile the PercolationStats file it will generate the result of the percolating probability threshold like this: (the result if you give the input 200 by 200 grid and try the simulation 100 times
~/Desktop/percolation> java-algs4 PercolationStats 200 100
mean = 0.5929934999999997
stddev = 0.00876990421552567
95% confidence interval = [0.5912745987737567, 0.5947124012262428]
but my result is a bunch of errors that:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "edu.princeton.cs.algs4.WeightedQuickUnionUF.find(int)" because "percolation.gridMap" is null
at percolation.percolates(percolation.java:93)
at PercolationStats.<init>(PercolationStats.java:17)
at PercolationStats.main(PercolationStats.java:58)\
I believe this is to be caused by the percolate datatype, and to be more specific is the line:
public boolean percolates() {
return gridMap.find(top) == gridMap.find(bottom);
}
It states that the gridMap variable of Union-Find is null - I don't know why this happens, I speculate this can relate to the scope of the "gridMap" variable, which is not applicable to all methods in the datatype. That is my hypothesis It could be wrong.
Can anyone give me a clue for why this happens and what should I fix for the successful compilation (I means to generate the probability)
Thanks in advance!
You have not set gridMap, though it is mentioned in a method called public int Percolation (int n). Perhaps you intended that Percolation is called from body of the constructor which you declared as public percolation(int n).
The fix should be simple: move the public int Percolation (int n) code into the constructor or make constructor public percolation(int n) call Percolation(n).
The class should follow conventions and renamed to Percolation.

I cannot get the write to file part of the code to work

I cannot get my write to file code working the error message- writeFile cannot be resolved. I a trying to write the board positions to a text file so the game can be saved. If the user chooses to save the game then it should call a new subroutine that will write the pieces to the file.
/*
* Skeleton program code for the AQA COMP1 Summer 2016 examination
* This code to be used in conjunction with the Preliminary Material
* written by the AQA Programmer Team
* Developed in the NetBeans 7.3.1. programming environment
* Additional classes AQAConsole2016, AQAReadTextFile2016 and
* AQAWriteTextFile2016 may be used.
*
* A package name may be chosen and private and public modifiers added -
* permission to make these changes to the Skeleton Program does not need
* to be obtained from AQA or the AQA Programmer
*/
import java.util.Random;
public class Aaa {
AQAConsole2016 console = new AQAConsole2016();
Random random = new Random();
int boardSize;
public Aaa() {
char choice;
String playerName;
// int boardSize;
boardSize = 6;
playerName = "";
do {
displayMenu();
choice = getMenuChoice(playerName);
switch (choice) {
case 'p' : playGame(playerName, boardSize);
break;
case 'e' : playerName = getPlayersName();
break;
case 'c' : boardSize = changeBoardSize();
break;
}
} while (choice != 'q');
}
void setUpGameBoard(char[][] board, int boardSize) {
for (int row = 1; row <= boardSize; row++) {
for (int column = 1; column <= boardSize; column++) {
if (row == (boardSize + 1) / 2 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2 + 1) {
board[row][column] = 'C';
} else {
if (row == (boardSize + 1) / 2 + 1 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2) {
board[row][column] = 'H';
} else {
board[row][column] = ' ';
}
}
}
}
}
int changeBoardSize() {
int boardSize;
do {
console.print("Enter a board size (between 4 and 9): ");
boardSize = console.readInteger("");
} while (!(boardSize >= 4 && boardSize <= 9));
return boardSize;
}
int getHumanPlayerMove(String playerName) {
int coordinates;
console.print(playerName + " enter the coordinates of the square where you want to place your piece: ");
coordinates = console.readInteger("");
return coordinates;
}
int getComputerPlayerMove(int boardSize) {
return ((random.nextInt(boardSize) + 1) * 10 + (random.nextInt(boardSize) + 1));
}
boolean gameOver(char[][] board, int boardSize) {
for (int row = 1; row <= boardSize; row++) {
for (int column = 1; column <= boardSize; column++) {
if (board[row][column] == ' ')
return false;
}
}
return true;
}
String getPlayersName() {
String playerName;
console.print("What is your name? ");
playerName = console.readLine();
return playerName;
}
boolean checkIfMoveIsValid(char[][] board, int move) {
int row;
int column;
boolean moveIsValid;
row = move % 10;
column = move / 10;
moveIsValid = false;
if (((row<=boardSize) &&(row>0)) && ((column<=boardSize) && (column>0))){
if (board[row][column] == ' ') {
moveIsValid = true;
}
}
return moveIsValid;
}
int getPlayerScore(char[][] board, int boardSize, char piece) {
int score;
score = 0;
for (int row = 1; row <= boardSize; row++) {
for (int column = 1; column <= boardSize; column++) {
if (board[row][column] == piece) {
score = score + 1;
}
}
}
return score;
}
boolean checkIfThereArePiecesToFlip(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
int rowCount;
int columnCount;
boolean flipStillPossible;
boolean flipFound;
boolean opponentPieceFound;
rowCount = startRow + rowDirection;
columnCount = startColumn + columnDirection;
flipStillPossible = true;
flipFound = false;
opponentPieceFound = false;
while (rowCount <= boardSize && rowCount >= 1 && columnCount >= 1 && columnCount <= boardSize && flipStillPossible && !flipFound ) {
if (board[rowCount][columnCount] == ' ') {
flipStillPossible = false;
} else {
if (board[rowCount][columnCount] != board[startRow][startColumn]) {
opponentPieceFound = true;
} else {
if (board[rowCount][columnCount] == board[startRow][startColumn] && !opponentPieceFound) {
flipStillPossible = false;
} else {
flipFound = true;
}
}
}
rowCount = rowCount + rowDirection;
columnCount = columnCount + columnDirection;
}
return flipFound;
}
void flipOpponentPiecesInOneDirection(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
int rowCount;
int columnCount;
boolean flipFound;
flipFound = checkIfThereArePiecesToFlip(board, boardSize, startRow, startColumn, rowDirection, columnDirection);
if (flipFound) {
rowCount = startRow + rowDirection;
columnCount = startColumn + columnDirection;
while (board[rowCount][columnCount] != ' ' && board[rowCount][columnCount] != board[startRow][startColumn]) {
if (board[rowCount][columnCount] == 'H') {
board[rowCount][columnCount] = 'C';
} else {
board[rowCount][columnCount] = 'H';
}
rowCount = rowCount + rowDirection;
columnCount = columnCount + columnDirection;
}
}
}
void makeMove(char[][] board, int boardSize, int move, boolean humanPlayersTurn) {
int row;
int column;
row = move % 10;
column = move / 10;
if (humanPlayersTurn) {
board[row][column] = 'H';
} else {
board[row][column] = 'C';
}
flipOpponentPiecesInOneDirection(board, boardSize, row, column, 1, 0);
flipOpponentPiecesInOneDirection(board, boardSize, row, column, -1, 0);
flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, 1);
flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, -1);
}
void printLine(int boardSize) {
console.print(" ");
for (int count = 1; count <= boardSize * 2 - 1; count++) {
console.print("_");
}
console.println();
}
void displayGameBoard(char[][] board, int boardSize) {
console.println();
console.print(" ");
for (int column = 1; column <= boardSize; column++)
{
console.print(" ");
console.print(column);
}
console.println();
printLine(boardSize);
for (int row = 1; row <= boardSize; row++) {
console.print(row);
console.print(" ");
for (int column = 1; column <= boardSize; column++) {
console.print("|");
console.print(board[row][column]);
}
console.println("|");
printLine(boardSize);
console.println();
}
}
void displayMenu() {
console.println("(p)lay game");
console.println("(e)nter name");
console.println("(c)hange board size");
console.println("(q)uit");
console.println();
}
char getMenuChoice(String playerName) {
char choice;
console.print(playerName + " enter the letter of your chosen option: ");
choice = console.readChar();
return choice;
}
void playGame(String playerName, int boardSize) {
char[][] board = new char[boardSize + 1][boardSize + 1];
boolean humanPlayersTurn;
int move;
int humanPlayerScore;
int computerPlayerScore;
boolean moveIsValid;
setUpGameBoard(board, boardSize);
humanPlayersTurn = false;
int NoOfMoves=0;
do {
humanPlayersTurn = !humanPlayersTurn;
displayGameBoard(board, boardSize);
moveIsValid = false;
do {
if (humanPlayersTurn) {
move = getHumanPlayerMove(playerName);
} else {
move = getComputerPlayerMove(boardSize);
}
moveIsValid = checkIfMoveIsValid(board, move);
} while (!moveIsValid);
if (!humanPlayersTurn) {
NoOfMoves++;
console.println("The number of moves completed so far: " +NoOfMoves);
console.print("Press the Enter key and the computer will make its move");
console.readLine("");
}
makeMove(board, boardSize, move, humanPlayersTurn);
console.println();
String answer = console.readLine("Do you want to save the board? (y/n)");
if (answer.equalsIgnoreCase("y")){
writeBoard(board, boardSize);
console.println("Saved!");
}
} while (!gameOver(board, boardSize));
displayGameBoard(board, boardSize);
humanPlayerScore = getPlayerScore(board, boardSize, 'H');
computerPlayerScore = getPlayerScore(board, boardSize, 'C');
if (humanPlayerScore > computerPlayerScore) {
console.println("Well done, " + playerName + ", you have won the game!");
}
else {
if (humanPlayerScore == computerPlayerScore) {
console.println("that was a draw!");
} else {
console.println("The computer has won the game!");
}
console.println();
}
}
void writeBoard(char[][] board, int boardSize) {
String filename = "myFile.txt";
String piece = null;
writeFile.openFile(filename);
for(int row=1; row<=boardSize; row++){
for (int column = 1; column <= boardSize; column++) {
piece= Character.toString(board [row][column]);
writeFile.writeToTextFile(piece);
}
}
writeFile.closeFile();
}
public static void main(String[] args) {
new Aaa();
}
}
This Section contains the errors:
void writeBoard(char[][] board, int boardSize) {
String filename = "myFile.txt";
String piece = null;
writeFile.openFile(filename);
for(int row=1; row<=boardSize; row++){
for (int column = 1; column <= boardSize; column++) {
piece= Character.toString(board [row][column]);
writeFile.writeToTextFile(piece);
}
}
writeFile.closeFile();
}
The problematic section contains a variable named writeFile which can not be resolved. No where in the class Aaa that you have shared, neither this variable has been declared nor initialized with an instance of its type. So the method void writeBoard(char[][] board, int boardSize) gives compilation error.
From the usage of this variable it is clear that it belongs to a class which has following instance methods:
1) openFile(String filename)
2) writeToTextFile(String piece)
3) closeFile()
Please search the class which has the above methods and on finding create an instance of that as the first statement inside the method void writeBoard(char[][] board, int boardSize). Hope it helps.

N-Queens using a stack, cannot find the bug

I have attempted to complete my homework project and am seeking help finding a bug. I am using a backtracking algorithm to find all the solutions to an N-queens problem. My main concern is my conflict method-which is inside a stack class. Its purpose is to detect if the Queen object (parameter 1 of conflict method) being passed is in the same row, column, or diagonal as any other queens on the board. The queen object passed into the conflict method is stored inside the Queen class and its location is recorded with the help of an instance of the Point class. My code uses two methods in the Queen class that I created, public int getRow(), and public int getColumn(). Both return an int. Second parameter is a 2d array (or array of arrays) named board. Queens already on the board are denoted in this array with a boolean value of true. Boolean values of false indicate an empty square on the board.
Solution.n is a reference to a static int variable in another class. Its value denotes the edge of the board. Example...for the 8-Queens problem we create a 2d array with size 8. Solution.n is decremented by 1 to equal the last index of the 2d array.
Here is the code:
public boolean conflict(Queen x, boolean [][] board) //conflict method
{
if(checkColumn(x, board) == false)
return true; //conflict
else if(checkRow(x, board) == false)
return true; //conflict
else if(checkDiagonal(x, board) == false )
return true; //conflict
else
return false; //no conflict on board
}
private boolean checkColumn(Queen x, boolean [][] board)//returns true when column is safe
{
int col = x.getColumn();
for(int row = 0; row <= Solution.n; row++)
{
if(board[row][col] == true) //queen is in this column
{
return false;
}
}
return true;
}
private boolean checkRow(Queen x, boolean [][] board) //returns true when row is safe
{
int row = x.getRow();
for(int col = 0; col <= Solution.n; col++)
{
if(board[row][col] == true) //queen is in this row
{
return false;
}
}
return true;
}
private boolean checkDiagonal(Queen location, boolean [][] board) //returns true when diagonal is safe
{
int row, col;
row = location.getRow() - 1;
col = location.getColumn() - 1;
while(row >=0 && col >= 0) //iterate down-left
{
if(board[row][col] == true) //queen found?
{
return false;
}
row--;
col--;
}
row = location.getRow() - 1;
col = location.getColumn() + 1;
while(row != -1 && col <= Solution.n) //iterate down-right
{
if(board[row][col] == true) //queen found?
{
return false;
}
row--;
col++;
}
row = location.getRow() + 1;
col = location.getColumn() + 1;
while(row <= Solution.n && col <= Solution.n) //iterate up-right
{
if(board[row][col] == true) //queen found?
{
return false;
}
row++;
col++;
}
row = location.getRow() +1;
col = location.getColumn()-1;
while(row <= Solution.n && col != -1) //iterate up-left
{
if(board[row][col] == true) //queen found?
{
return false;
}
row++;
col--;
}
return true;
}
I am convinced this snippet of code contains a bug, but if i'm wrong then I apologize for wasting your time :P
Your help would be greatly appreciated. Thanks! :D
You have several small bugs in there - for example, you have loops that go from 0 to Solution.n, inclusive, while they should go to Solution.n-1. Most of the errors, however, can be eliminated by picking a more suitable data structure.
Think about it: you don't need a full NxN board to decide the placement of a queen:
There's one queen per row, so queen's number is its row.
There's one queen per column, so you need an array of boolean[N] to know which rows are taken.
There's one queen per ascending diagonal, so you need an array of boolean[2N-1] to know which ascending diagonals are taken.
There's one queen per descending diagonal, so you need an array of boolean[2N-1] to know which descending diagonals are taken.
boolean[] columns = new boolean[N];
boolean[] ascending = new boolean[2*N-1];
boolean[] descending = new boolean[2*N-1];
At this point you've got all you need: instead of a square boolean[N][N] array you need three linear arrays of boolean. This lets you do your checks much faster, too:
int c = x.getColumn();
int r = x.getRow();
boolean conflict = columns[c]
|| ascending[r+c]
|| descending[N-r+c];
That's it - no loops required! Now you can code your backtracking algorithm using these three arrays instead of a square board.
This answer won't solve your problem, since I'm not convinced your error is in the code you pasted, but here's your code, written a bit closer to how I might write it:
// returns true when column is safe
private boolean checkColumn(Queen x, boolean [][] board)
{
int col = x.getColumn();
for(int row = 0; row <= Solution.n; row++)
{
if(board[row][col]){ return false; }
}
return true;
}
// returns true when row is safe
private boolean checkRow(Queen x, boolean [][] board)
{
int row = x.getRow();
for(int col = 0; col <= Solution.n; col++)
{
if(board[row][col]){ return false; }
}
return true;
}
// returns true if the position is valid given the board size
// (as defined by Solution)
private boolean validPosition(int row, int col)
{
if(0 > row || row > Solution.n){ return false; }
if(0 > col || col > Solution.n){ return false; }
return true;
}
// returns true when diagonal is safe
private boolean checkDiagonal(Queen x, boolean [][] board)
{
int row, col;
// Down Left
row = x.getRow(); // "Start" on current position
col = x.getColumn();
while(true)
{
row--; col--; // Take a step in the direction
if(!validPosition(row, col)){ break; } // Stop if we've left the board
if(board[row][col]){ return false; } // Check whether it's occupied
}
// Down Right
row = x.getRow();
col = x.getColumn();
while(true)
{
row--; col++;
if(!validPosition(row, col)){ break; }
if(board[row][col]){ return false; }
}
// Up Right
row = x.getRow();
col = x.getColumn();
while(true)
{
row++; col++;
if(!validPosition(row, col)){ break; }
if(board[row][col]){ return false; }
}
// Up Left
row = x.getRow();
col = x.getColumn();
while(true)
{
row++; col--;
if(!validPosition(row, col)){ break; }
if(board[row][col]){ return false; }
}
return true;
}
public boolean conflict(Queen x, boolean [][] board) //conflict method
{
if ( checkColumn(x, board) == false){ return true; }
else if( checkRow(x, board) == false){ return true; }
else if(checkDiagonal(x, board) == false){ return true; }
else { return false; }
}
}
It simplifies a lot of the logic, adds a helper function validPosition(), and cleans up some of the tests and loops.

Sudoku solver backtracking

I'm browsing through some sudoku solvers and I'm looking for one that utilizes backtracking, now I've found this code but I'm not really sure whether it uses backtracking or some other algorithm?
Help is appreciated.
abstract class SudoKiller {
private SudokuBoard sb; // Puzzle to solve;
public SudoKiller(SudokuBoard sb) {
this.sb = sb;
}
private boolean check(int num, int row, int col) {
int r = (row / sb.box_size) * sb.box_size;
int c = (col / sb.box_size) * sb.box_size;
for (int i = 0; i < sb.size; i++) {
if (sb.getCell(row, i) == num ||
sb.getCell(i, col) == num ||
sb.getCell(r + (i % sb.box_size), c + (i / sb.box_size)) == num) {
return false;
}
}
return true;
}
public boolean guess(int row, int col) {
int nextCol = (col + 1) % sb.size;
int nextRow = (nextCol == 0) ? row + 1 : row;
try {
if (sb.getCell(row, col) != sb.EMPTY)
return guess(nextRow, nextCol);
}
catch (ArrayIndexOutOfBoundsException e) {
return true;
}
for (int i = 1; i <= sb.size; i++) {
if (check(i, row, col)) {
sb.setCell(i, row, col);
if (guess(nextRow, nextCol)) {
return true;
}
}
}
sb.setCell(sb.EMPTY, row, col);
return false;
}
}
And if this is not backtracking, is there an easy way to "convert" to it?
The whole project can be found on the authors site.
Looks like it does backtracking via recursion.
Here we step forward:
sb.setCell(i, row, col);
and here we step backward:
sb.setCell(sb.EMPTY, row, col);

Problems with the N queen homework exercice in Java

I'm using my own implementation of the stack.
I should not use recursion.
My code:
public static void solve(int bsize)
{
stack queenLoc = new stack();
int y=0;
int count=0;
boolean done = false;
while(done == false && queenLoc.size() != bsize)
{
queenLoc.push(count);
if(!isSafe(bsize,queenLoc,count))
{
while(queenLoc.getTop() == bsize)
{
y = queenLoc.pop();
count--;
}
if(queenLoc.top != null)
{
queenLoc.push(queenLoc.pop()+1);
count++;
}
else
{
queenLoc.push(y+1);
count++;
}
}
else if(queenLoc.size() == bsize)
{
done = true;
}
else
{
count++;
queenLoc.push(count);
}
}
queenLoc.showAll();
if(queenLoc.size() == bsize)
printBoard(bsize, queenLoc);
}
public static boolean isSafe(int bsize, stack s,int count)
{
for(int i = 1; i<s.size(); i++)
{
if(s.getTop() == s.get(i) || s.getTop()+count == s.get(i)+s.size() || s.getTop()-count == s.get(i)-s.size())
return false;
}
return true;
}
I'm not sure what is really going on, i'm getting wrong position and the printBoard function is only printing the queens on the first row.
I actually tried a lot of possibilities, but i got a bit confused.
Can anyone just point me out to the right direction and tell me where's the problem in my code. I am using the stack to store the column and the "count" variable in the stack class to point me to which row.
package mynqueens;
public class MyNQueens {
public static int board[][] = new int[4][4];
public static int row,column;
public MyNQueens(){
}
public static void main(String[] args) {
check(0,0);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
}
public static void check(int i, int j ){
while(i<3){
board[i][j] = 1;
i++;
}
while(i!=0){
board[i][j] = 1;
i--;
}
while(j<3){
board[i][j]=1;
j++;
}
while(j!=0){
board[i][j] = 1;
j--;
}
while(j<3 || i<3){
board[i][j] = 1;
i++;
j++;
}
while(j!=0 || i!=0){
board[i][j] = 1;
i--;
j--;
}
while(i<3 || j!=0){
board[i][j]=1;
i++;
j--;
}
while(i!=0 || j<3){
board[i][j]=1;
i--;
j++;
}
}
}
Before you start erasing elements in inner while section
while(queenLoc.getTop() == bsize)
{
y = queenLoc.pop();
count--;
}
number of elements in queenStack will exceed bsize.
You have while(done == false && queenLoc.size() != bsize) so when size of queenLoc will be equal to bsize you will print result.
What I am saying is that after bsize steps you are always printing results.
Advise: Your code should have invariant "queenLoc represents position where no two queens attack each other".
import java.util.Scanner;
/**
*
* #author Manimekalai
*/
public class Queen {
public static boolean isConsistent(int[] q, int n)
{
for (int i = 0; i < n; i++)
{
if (q[i] == q[n]) return false; // same column
if ((q[i] - q[n]) == (n - i)) return false; // same major diagonal
if ((q[n] - q[i]) == (n - i)) return false; // same minor diagonal
}
return true;
}
public static void printQueens(int[] q)
{
int N = q.length;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (q[i] == j) System.out.print("Q ");
else System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static void enumerate(int N)
{
int[] a = new int[N];
enumerate(a, 0);
}
public static void enumerate(int[] q, int n)
{
int N = q.length;
if (n == N) printQueens(q);
else
{
for (int i = 0; i < N; i++)
{
q[n] = i;
if (isConsistent(q, n)) enumerate(q, n+1);
}
}
}
public static void main(String[] args)
{
//int N = Integer.parseInt(args[0]);
System.out.println("Enter N value");
Scanner s=new Scanner(System.in);
int N=s.nextInt();
enumerate(N);
}
}

Categories