1 /* |
|
2 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 import java.util.*; |
|
26 |
|
27 // Copyright 1996, Animorphic Systems |
|
28 // gri 28 Aug 92 / 15 Jan 93 / 8 Dec 95 |
|
29 |
|
30 class Queens { |
|
31 |
|
32 static void try_i(boolean a[], boolean b[], boolean c[], int x[], int i) { |
|
33 int adj = 7; |
|
34 |
|
35 for (int j = 1; j <= 8; j++) { |
|
36 if (b[j] && a[i+j] && c[adj+i-j]) { |
|
37 x[i] = j; |
|
38 b[j] = false; |
|
39 a[i+j] = false; |
|
40 c[adj+i-j] = false; |
|
41 if (i < 8) try_i(a, b, c, x, i+1); |
|
42 else print(x); |
|
43 b[j] = true; |
|
44 a[i+j] = true; |
|
45 c[adj+i-j] = true; |
|
46 } |
|
47 } |
|
48 } |
|
49 |
|
50 public static void main(String s[]) { |
|
51 boolean a[] = new boolean[16+1]; |
|
52 boolean b[] = new boolean[ 8+1]; |
|
53 boolean c[] = new boolean[14+1]; |
|
54 int x[] = new int[8+1]; |
|
55 int adj = 7; |
|
56 |
|
57 for (int i = -7; i <= 16; i++) { |
|
58 if (i >= 1 && i <= 8) b[i] = true; |
|
59 if (i >= 2) a[i] = true; |
|
60 if (i <= 7) c[adj+i] = true; |
|
61 } |
|
62 |
|
63 x[0] = 0; // solution counter |
|
64 |
|
65 try_i(a, b, c, x, 1); |
|
66 } |
|
67 |
|
68 static void print(int x[]) { |
|
69 // first correct solution: A1 B5 C8 D6 E3 F7 G2 H4 |
|
70 |
|
71 char LF = (char)0xA; |
|
72 char CR = (char)0xD; |
|
73 |
|
74 x[0]++; |
|
75 if (x[0] < 10) |
|
76 System.out.print(" "); |
|
77 System.out.print(x[0] + ". "); |
|
78 for (int i = 1; i <= 8; i++) { |
|
79 char p = (char)('A' + i - 1); |
|
80 System.out.print(p); |
|
81 System.out.print (x[i] + " "); |
|
82 } |
|
83 System.out.println(); |
|
84 } |
|
85 |
|
86 }; |
|