make/test/Queens.java

changeset 526
a294fd0c4b38
parent 435
a61af66fc99e
child 631
d1605aabd0a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make/test/Queens.java	Wed Apr 09 14:22:48 2008 -0400
     1.3 @@ -0,0 +1,86 @@
     1.4 +/*
     1.5 + * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +import java.util.*;
    1.29 +
    1.30 +// Copyright 1996, Animorphic Systems
    1.31 +// gri 28 Aug 92 / 15 Jan 93 / 8 Dec 95
    1.32 +
    1.33 +class Queens {
    1.34 +
    1.35 +  static void try_i(boolean a[], boolean b[], boolean c[], int x[], int i) {
    1.36 +    int adj = 7;
    1.37 +
    1.38 +    for (int j = 1; j <= 8; j++) {
    1.39 +      if (b[j] && a[i+j] && c[adj+i-j]) {
    1.40 +        x[i] = j;
    1.41 +        b[j] = false;
    1.42 +        a[i+j] = false;
    1.43 +        c[adj+i-j] = false;
    1.44 +        if (i < 8) try_i(a, b, c, x, i+1);
    1.45 +        else print(x);
    1.46 +        b[j] = true;
    1.47 +        a[i+j] = true;
    1.48 +        c[adj+i-j] = true;
    1.49 +      }
    1.50 +    }
    1.51 +  }
    1.52 +
    1.53 +  public static void main(String s[]) {
    1.54 +    boolean a[] = new boolean[16+1];
    1.55 +    boolean b[] = new boolean[ 8+1];
    1.56 +    boolean c[] = new boolean[14+1];
    1.57 +    int     x[] = new int[8+1];
    1.58 +    int adj = 7;
    1.59 +
    1.60 +    for (int i = -7; i <= 16; i++) {
    1.61 +      if (i >= 1 && i <= 8) b[i]     = true;
    1.62 +      if (i >= 2)           a[i]     = true;
    1.63 +      if (i <= 7)           c[adj+i] = true;
    1.64 +    }
    1.65 +
    1.66 +    x[0] = 0; // solution counter
    1.67 +
    1.68 +    try_i(a, b, c, x, 1);
    1.69 +  }
    1.70 +
    1.71 +  static void print(int x[]) {
    1.72 +    // first correct solution: A1 B5 C8 D6 E3 F7 G2 H4
    1.73 +
    1.74 +    char LF = (char)0xA;
    1.75 +    char CR = (char)0xD;
    1.76 +
    1.77 +    x[0]++;
    1.78 +    if (x[0] < 10)
    1.79 +        System.out.print(" ");
    1.80 +    System.out.print(x[0] + ". ");
    1.81 +    for (int i = 1; i <= 8; i++) {
    1.82 +      char p = (char)('A' + i - 1);
    1.83 +      System.out.print(p);
    1.84 +      System.out.print (x[i] + " ");
    1.85 +    }
    1.86 +    System.out.println();
    1.87 +  }
    1.88 +
    1.89 +};

mercurial