Difference between revisions of "Eight Queens"

From John Senner
Jump to navigation Jump to search
(Created page with "== SQL Eight Queens in SQL == '''Create the board:''' <source lang="sql"> CREATE TABLE rows ( id integer PRIMARY KEY ); INSERT INTO rows (id) VALUES (1),(2),(3),(4),(5),(6)...")
 
 
Line 1: Line 1:
== SQL Eight Queens in SQL ==
+
<pre>
 +
-- SQL Eight Queens in SQL
  
'''Create the board:'''
+
-- Create the board:
<source lang="sql">
 
 
CREATE TABLE rows (
 
CREATE TABLE rows (
 
   id integer PRIMARY KEY
 
   id integer PRIMARY KEY
 
);
 
);
INSERT INTO rows (id) VALUES (1),(2),(3),(4),(5),(6),(7),(8);
+
INSERT INTO rows (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);
  
 
CREATE TABLE cols (
 
CREATE TABLE cols (
 
   id integer PRIMARY KEY
 
   id integer PRIMARY KEY
 
);
 
);
INSERT INTO cols (id) VALUES (1),(2),(3),(4),(5),(6),(7),(8);
+
INSERT INTO cols (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);
</source>
 
  
'''Get a set of queens:'''
+
-- Get a set of queens:
<source lang="sql">
 
 
SELECT
 
SELECT
 
   cols.id AS col1, rows.id AS row1,
 
   cols.id AS col1, rows.id AS row1,
Line 58: Line 56:
 
             col8, row8,
 
             col8, row8,
 
             rows.id AS row7, cols.id AS col7
 
             rows.id AS row7, cols.id AS col7
             FROM rows, cols, (SELECT
+
             FROM rows, cols
              rows.id AS row8, cols.id AS col8
+
             WHERE cols.id != col8 AND rows.id != row8 -- This checks rook moves
              FROM rows, cols)
+
             AND (cols.id + rows.id != col8 + row8) -- This checks bishop moves
            AS b8
 
             WHERE cols.id != col8 AND rows.id != row8
 
             AND (cols.id + rows.id != col8 + row8)
 
 
             AND (cols.id - rows.id != col8 - row8)
 
             AND (cols.id - rows.id != col8 - row8)
            ) AS b7
+
          ) AS b7
 
           WHERE cols.id != col8 AND rows.id != row8
 
           WHERE cols.id != col8 AND rows.id != row8
 
           AND cols.id != col7 AND rows.id != row7
 
           AND cols.id != col7 AND rows.id != row7
 
           AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 
           AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 
           AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 
           AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
          ) AS b6
+
        ) AS b6
 
         WHERE cols.id != col8 AND rows.id != row8
 
         WHERE cols.id != col8 AND rows.id != row8
 
         AND cols.id != col7 AND rows.id != row7
 
         AND cols.id != col7 AND rows.id != row7
Line 77: Line 72:
 
         AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 
         AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 
         AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
 
         AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
        ) AS b5
+
      ) AS b5
 
       WHERE cols.id != col8 AND rows.id != row8
 
       WHERE cols.id != col8 AND rows.id != row8
 
       AND cols.id != col7 AND rows.id != row7
 
       AND cols.id != col7 AND rows.id != row7
Line 86: Line 81:
 
       AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
 
       AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
 
       AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
 
       AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
      ) AS b4
+
    ) AS b4
 
     WHERE cols.id != col8 AND rows.id != row8
 
     WHERE cols.id != col8 AND rows.id != row8
 
     AND cols.id != col7 AND rows.id != row7
 
     AND cols.id != col7 AND rows.id != row7
Line 97: Line 92:
 
     AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
 
     AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
 
     AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
 
     AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
    ) AS b3
+
  ) AS b3
 
   WHERE cols.id != col8 AND rows.id != row8
 
   WHERE cols.id != col8 AND rows.id != row8
 
   AND cols.id != col7 AND rows.id != row7
 
   AND cols.id != col7 AND rows.id != row7
Line 104: Line 99:
 
   AND cols.id != col4 AND rows.id != row4
 
   AND cols.id != col4 AND rows.id != row4
 
   AND cols.id != col3 AND rows.id != row3
 
   AND cols.id != col3 AND rows.id != row3
  AND cols.id != col2 AND rows.id != row2
 
 
   AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 
   AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 
   AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 
   AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
Line 111: Line 105:
 
   AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
 
   AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
 
   AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
 
   AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
  AND (cols.id + rows.id != col2 + row2) AND (cols.id - rows.id != col2 - row2)
+
) AS b2
LIMIT 1337; --Arbitrary, you can let yours go all day.
+
WHERE cols.id != col8 AND rows.id != row8
</source>
+
AND cols.id != col7 AND rows.id != row7
 
+
AND cols.id != col6 AND rows.id != row6
''Commentary:'' I realize that this could be more elegant by trimming out the hard-coded values, and that I could set it up for N queens, but I got excited when it ran for 8. I wrote a nonrecursive brute-force version that ended as expected, with me sighing and restarting Postgres. If I go and edit it, it'll certainly be to put the results in a human-readable form. Because it's really cool, but isn't smart enough to choose good placements ahead of time, I give myself 7 Queens out of a possible 8. Also, update since 2007, ChatGPT will just do this for you.
+
AND cols.id != col5 AND rows.id != row5
 +
AND cols.id != col4 AND rows.id != row4
 +
AND cols.id != col3 AND rows.id != row3
 +
AND cols.id != col2 AND rows.id != row2
 +
AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
 +
AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
 +
AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
 +
AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
 +
AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
 +
AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
 +
AND (cols.id + rows.id != col2 + row2) AND (cols.id - rows.id != col2 - row2)
 +
LIMIT 1337; --Arbitrary, you can let yours go all day.
 +
-- Note that this won't return very many unique solutions (unless your queens have numbers written on them)
 +
</pre>

Latest revision as of 23:50, 15 May 2024

-- SQL Eight Queens in SQL

-- Create the board:
CREATE TABLE rows (
  id integer PRIMARY KEY
);
INSERT INTO rows (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);

CREATE TABLE cols (
  id integer PRIMARY KEY
);
INSERT INTO cols (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);

-- Get a set of queens:
SELECT
  cols.id AS col1, rows.id AS row1,
  col2, row2,
  col3, row3,
  col4, row4,
  col5, row5,
  col6, row6,
  col7, row7,
  col8, row8
FROM rows, cols, (SELECT
  col3, row3,
  col4, row4,
  col5, row5,
  col6, row6,
  col7, row7,
  col8, row8,
  rows.id AS row2, cols.id AS col2
  FROM rows, cols, (SELECT
    col4, row4,
    col5, row5,
    col6, row6,
    col7, row7,
    col8, row8,
    rows.id AS row3, cols.id AS col3
    FROM rows, cols, (SELECT
      col5, row5,
      col6, row6,
      col7, row7,
      col8, row8,
      rows.id AS row4, cols.id AS col4
      FROM rows, cols, (SELECT
        col6, row6,
        col7, row7,
        col8, row8,
        rows.id AS row5, cols.id AS col5
        FROM rows, cols, (SELECT
          col7, row7,
          col8, row8,
          rows.id AS row6, cols.id AS col6
          FROM rows, cols, (SELECT
            col8, row8,
            rows.id AS row7, cols.id AS col7
            FROM rows, cols
            WHERE cols.id != col8 AND rows.id != row8 -- This checks rook moves
            AND (cols.id + rows.id != col8 + row8) -- This checks bishop moves
            AND (cols.id - rows.id != col8 - row8)
          ) AS b7
          WHERE cols.id != col8 AND rows.id != row8
          AND cols.id != col7 AND rows.id != row7
          AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
          AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
        ) AS b6
        WHERE cols.id != col8 AND rows.id != row8
        AND cols.id != col7 AND rows.id != row7
        AND cols.id != col6 AND rows.id != row6
        AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
        AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
        AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
      ) AS b5
      WHERE cols.id != col8 AND rows.id != row8
      AND cols.id != col7 AND rows.id != row7
      AND cols.id != col6 AND rows.id != row6
      AND cols.id != col5 AND rows.id != row5
      AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
      AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
      AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
      AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
    ) AS b4
    WHERE cols.id != col8 AND rows.id != row8
    AND cols.id != col7 AND rows.id != row7
    AND cols.id != col6 AND rows.id != row6
    AND cols.id != col5 AND rows.id != row5
    AND cols.id != col4 AND rows.id != row4
    AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
    AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
    AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
    AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
    AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
  ) AS b3
  WHERE cols.id != col8 AND rows.id != row8
  AND cols.id != col7 AND rows.id != row7
  AND cols.id != col6 AND rows.id != row6
  AND cols.id != col5 AND rows.id != row5
  AND cols.id != col4 AND rows.id != row4
  AND cols.id != col3 AND rows.id != row3
  AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
  AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
  AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
  AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
  AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
  AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
) AS b2
WHERE cols.id != col8 AND rows.id != row8
AND cols.id != col7 AND rows.id != row7
AND cols.id != col6 AND rows.id != row6
AND cols.id != col5 AND rows.id != row5
AND cols.id != col4 AND rows.id != row4
AND cols.id != col3 AND rows.id != row3
AND cols.id != col2 AND rows.id != row2
AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
AND (cols.id + rows.id != col2 + row2) AND (cols.id - rows.id != col2 - row2)
LIMIT 1337; --Arbitrary, you can let yours go all day.
-- Note that this won't return very many unique solutions (unless your queens have numbers written on them)