Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.3 kB
3
Indexable
Never
public class Main {

    int[] x;

    public Main(int N) {
        x = new int[N];
    }

    public boolean canPlaceQueen(int r, int c) {

        for (int i = 0; i < r; i++) {
            if (x[i] == c || (i - r) == (x[i] - c) || (i - r) == (c - x[i])) {
                return false;
            }
        }
        return true;
    }

    public void printQueens(int[] x) {
        int N = x.length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (x[i] == j) {
                    System.out.print("Q ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }

    public void placeNqueens(int r, int n) {
        for (int c = 0; c < n; c++) {
            if (canPlaceQueen(r, c)) {
                x[r] = c;
                if (r == n - 1) {
                    printQueens(x);
                } else {
                    placeNqueens(r + 1, n);
                }
            }
        }
    }

    public void callplaceNqueens() {
        placeNqueens(0, x.length);
    }

    public static void main(String args[]) {
        Main Q = new Main(4);
        Q.callplaceNqueens();

    }

}