Java progrma to solve NQueens Problem using Staircase Algorithm.

```import java.util.Scanner;

// 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main {
private static char[][] board;

public static void main(String[] args) {
int n;

System.out.println("Enter size of a board");

setBoard(n);
placeQueens(n);
printBoard();
}

private static void setBoard(int n){
board = new char[n][n];

for(int y = 0; y < n; ++y){
for(int x = 0; x < n; ++x){
board[y][x] = 'X';
}
}
}

private static void placeQueens(int n){
if(n % 2 == 1){
board[n - 1][n - 1] = 'Q';
--n;
}

if(n % 6 != 2){
for(int i = 1; i <= n / 2; ++i){
board[i - 1][2 * i - 1] = 'Q';
board[n / 2 + i - 1][2 * i - 2] = 'Q';
}
}else{
for(int i = 1; i <= n / 2; ++i){
board[i - 1][((2 * i + n / 2 - 3) % n)] = 'Q';
board[n - i][n - 1 - ((2 * i + n / 2 - 3) % n)] = 'Q';
}
}
}

private static void printBoard(){
for(int i = 0; i < board.length; ++i){
for (int j = 0; j < board.length; ++j){
System.out.print(board[i][j]);
}

System.out.println();
}

System.out.println();
}
}
```
```IN
10
OUT
XQXXXXXXXX
XXXQXXXXXX
XXXXXQXXXX
XXXXXXXQXX
XXXXXXXXXQ
QXXXXXXXXX
XXQXXXXXXX
XXXXQXXXXX
XXXXXXQXXX
XXXXXXXXQX```

Java NQueens Problem Staircase Algorithm
Tagged on: