package UE11_140125_Rekursion4;

public class EightQueens {

    public static final int dim = 8;
    public static int counter = 0;

    public static int[] columns = new int[dim];
    public static boolean[] isBlockedRow = new boolean[dim];

    public static boolean[] isBlockedD1 = new boolean[2 * dim - 1];
    public static boolean[] isBlockedD2 = new boolean[2 * dim - 1];

    public static void main(String[] args) {
        boolean giveDetailedOutput = false;
        tower(0, giveDetailedOutput);
        System.out.println(counter);
        counter = 0;
        queen(0, giveDetailedOutput);
        System.out.println(counter);
    }

    public static void tower(int column, boolean giveDetailedOutput) {
        if (column == dim) {
            counter++;
            if (giveDetailedOutput) output();
            return;
        }
        for (int line = 0; line < dim; line++) {
            if (isBlockedRow[line]) continue;
            isBlockedRow[line] = true;
            columns[column] = line;
            tower(column + 1, giveDetailedOutput);
            isBlockedRow[line] = false;
        }
    }

    public static void queen(int column, boolean giveDetailedOutput) {
        if (column >= dim) {
            counter++;
            if (giveDetailedOutput) output();
            return;
        }
        for (int row = 0; row < dim; row++) {
            int d1 = row + column, d2 = (dim - 1) - row + column;
            if (isBlockedRow[row] || isBlockedD1[d1] || isBlockedD2[d2]) continue;
            isBlockedRow[row] = isBlockedD1[d1] = isBlockedD2[d2] = true;
            columns[column] = row;
            queen(column + 1, giveDetailedOutput);
            isBlockedRow[row] = isBlockedD1[d1] = isBlockedD2[d2] = false;
        }
    }

    public static void output() {
        for (int i = 0; i < columns.length; i++) {
            char c = (char) ('A' + i);
            System.out.print(c + "" + (columns[i] + 1) + " ");
        }
        System.out.println();
    }
}
