For homework I was assigned the "non-attacking queens" problem.

Now I have all_boards as a static variable and i was trying to store each step as a board. like row 1 board, row 2 board, etc, but for some reason all the boards are overwritten with the final board.

My plan for this was to try to place a queen, if it didn't place a queen, then it takes the board from 2 steps up, moves and then trys to replace the queen 1 step up, but not in the same position as before.

Can anyone help me find out where I went wrong?

public class nonqueen
{
	public static char[][][] all_boards = new char[8][8][8];
	public static void main (String args[])
	{
		int size = 8;
		for(int k=0; k<size; k++)
		{
			for (int i=0; i<size; i++)
			{
				for (int j=0; j<size; j++)
				{
					all_boards[k][i][j] = '0';
				}
			}
		}
		placequeen(0, 0);
		for(int k=0; k<size; k++)
		{
			for (int i=0; i<size; i++)
			{
				for (int j=0; j<size; j++)
				{
					System.out.print(all_boards[k][i][j] + " ");
				}
				System.out.println();
			}
			System.out.println();
		}
	}	
	public static void placequeen(int row, int start_col)
	{
		int placed = 0;
		for (int j=start_col; j<8; j++)
		{
			if (all_boards[row][row][j] != 'X' && all_boards[row][row][j] != '+' && placed != 1) //X = Queen, '+' = Path of queens
			{
				placed = 1;
				all_boards[row][row][j] = 'X';
				for (int k=row; k<8; k++)
				{
					if (all_boards[row][k][j] != 'X')
					{
						all_boards[row][k][j] = '+';
					}
				}
				int I = 1;
				while ((j+I) < 8 && (row+I) < 8)
				{
					all_boards[row][row+I][j+I] = '+';
					I++;
				}
				I = 1;
				while ((j-I) >= 0 && (row+I) < 8)
				{
					all_boards[row][row+I][j-I] = '+';
					I++;
				}
				int J = 1;
			}
			else
				all_boards[row][row][j] = '+';
		}
		if (placed == 0 && row > 1)
			{
				start_col = queen_at(all_boards[row-1], (row-1));
				all_boards[row-1] = all_boards[row-2];
				all_boards[row] = all_boards[row-2];
				if (start_col < 7)
					placequeen(row-1, start_col+1);
			}
		else if (row+1 < 3 && placed == 1)
		{
			all_boards[row+1] = all_boards[row];
			placequeen((row+1), 0);
		}
/*		else
		{
			if (row > 1)
			{
				if (queen_at(board, row-1) < 7)
					placequeen(row-1, (queen_at(all_boards[row-1], row)+1));
				all_boards[row-1] = all_boards[row-2];
			}
		}
*/	}
	public static int queen_at(char[][] board, int row)
	{
		int j = 9;
		for (int i=0; i<8; i++)
		{
			if (all_boards[row][row][i] == 'X')
				j = i;
		}
		return j;
	}
}

oh, dont mind some of the things I have placed in there, I was just messing with things to make checks, like when i have it say if its less than 3 towards the end.

People too often assume that all programmers are familiar with these oddly named puzzles. Would you explain what the "non-attacking queens" problem is?

I'm sorry, lol. The non attacking queen problem is having a chess board setup where you have to place 8 queens on an 8x8 board, where no queen can attack another.

(In my script I have the paths placed only going down because i start from the top row and work my way down, so i have no need to put the paths of other queens going up.)

This article has been dead for over six months. Start a new discussion instead.