<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">public class Queen
{
   private int size; // dimension of the board
   private int file; // the fixed file for this queen
   private int rank; // the variable rank for this queen
   private Queen left, right; // the neighboring queens

   /* Create yourself on a board of the specified size, in
   the specified file, and all the queens to your right, so that
   each queen knows its immediate neighbors */
   public Queen(int sizeIn, int fileIn, Queen leftIn) {
      size = sizeIn;
      file = fileIn;
      left = leftIn;
      if (file &lt; size)
         right = new Queen(size, file+1, this);
   }

   /* Try placing yourself in all possible ranks */
   public void place() {
      for (rank=1; rank&lt;=size; ++rank)
         if (file==1 || !left.attack(file,rank))
	    if (file&lt;size) right.place();
	    else print();
   }

   /* Ask the queens to your left to print their locations, then
   print your own location */
   public void print() {
      if (file&gt;1) left.print();
      System.out.print(" "+this);
      if (file==size) System.out.println();
   }

   /* Do you or any queens to your left attack this square? */
   private boolean attack(int f, int r) {
      return inSight(file,rank,f,r) || (file&gt;1 &amp;&amp; left.attack(f,r));
   }

   /* Are the two squares in sight of each other?  That is, are they
   on the same rank or in the same diagonal? It's assumed they're in
   different files */
   private static boolean inSight(int f1, int r1, int f2, int r2) {
      return r1==r2 || Math.abs(f1-f2)==Math.abs(r1-r2);
   }

   public String toString() {
      return "["+rank+","+file+"]";
   }
}
</pre></body></html>