duke@435: /* trims@1907: * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: import java.util.*; duke@435: duke@435: // Copyright 1996, Animorphic Systems duke@435: // gri 28 Aug 92 / 15 Jan 93 / 8 Dec 95 duke@435: duke@435: class Queens { duke@435: duke@435: static void try_i(boolean a[], boolean b[], boolean c[], int x[], int i) { duke@435: int adj = 7; duke@435: duke@435: for (int j = 1; j <= 8; j++) { duke@435: if (b[j] && a[i+j] && c[adj+i-j]) { duke@435: x[i] = j; duke@435: b[j] = false; duke@435: a[i+j] = false; duke@435: c[adj+i-j] = false; duke@435: if (i < 8) try_i(a, b, c, x, i+1); duke@435: else print(x); duke@435: b[j] = true; duke@435: a[i+j] = true; duke@435: c[adj+i-j] = true; duke@435: } duke@435: } duke@435: } duke@435: duke@435: public static void main(String s[]) { duke@435: boolean a[] = new boolean[16+1]; duke@435: boolean b[] = new boolean[ 8+1]; duke@435: boolean c[] = new boolean[14+1]; duke@435: int x[] = new int[8+1]; duke@435: int adj = 7; duke@435: duke@435: for (int i = -7; i <= 16; i++) { duke@435: if (i >= 1 && i <= 8) b[i] = true; duke@435: if (i >= 2) a[i] = true; duke@435: if (i <= 7) c[adj+i] = true; duke@435: } duke@435: duke@435: x[0] = 0; // solution counter duke@435: duke@435: try_i(a, b, c, x, 1); duke@435: } duke@435: duke@435: static void print(int x[]) { duke@435: // first correct solution: A1 B5 C8 D6 E3 F7 G2 H4 duke@435: duke@435: char LF = (char)0xA; duke@435: char CR = (char)0xD; duke@435: duke@435: x[0]++; duke@435: if (x[0] < 10) duke@435: System.out.print(" "); duke@435: System.out.print(x[0] + ". "); duke@435: for (int i = 1; i <= 8; i++) { duke@435: char p = (char)('A' + i - 1); duke@435: System.out.print(p); duke@435: System.out.print (x[i] + " "); duke@435: } duke@435: System.out.println(); duke@435: } duke@435: duke@435: };