トップページに戻る    次のSQLパズルへ    前のSQLパズルへ

11-15 8王妃問題

SQLパズル

Eight queens puzzleを解きます。

出力結果
  A    B    C    D    E    F    G    H
---  ---  ---  ---  ---  ---  ---  ---
  1    5    8    6    3    7    2    4
  1    6    8    3    7    4    2    5
  1    7    4    6    8    2    5    3
  1    7    5    8    2    4    6    3
  2    4    6    8    3    1    7    5
  2    5    7    1    3    8    6    4
  2    5    7    4    1    8    6    3
  2    6    1    7    4    8    3    5
  2    6    8    3    1    4    7    5
  2    7    3    6    8    5    1    4
  2    7    5    8    1    4    6    3
  2    8    6    1    3    5    7    4
  3    1    7    5    8    2    4    6
  3    5    2    8    1    7    4    6
  3    5    7    1    4    2    8    6
  3    5    8    4    1    7    2    6
  3    6    2    5    8    1    7    4
  3    6    2    7    1    4    8    5
  3    6    2    7    5    1    8    4
  3    6    8    1    5    7    2    4
  3    6    8    2    4    1    7    5
  3    7    2    8    5    1    4    6
  3    7    2    8    6    4    1    5
  3    8    4    7    1    6    2    5
  4    1    5    8    2    7    3    6
  4    2    5    8    6    1    3    7
  4    2    7    3    6    8    1    5
  4    2    8    5    7    1    3    6
  4    2    8    6    1    3    5    7
  4    6    1    5    2    8    3    7
  4    6    8    2    7    1    3    5
  4    7    3    8    2    5    1    6
  4    7    5    2    6    1    3    8
  4    8    1    3    6    2    7    5
  4    8    5    3    1    7    2    6
  5    1    8    4    2    7    3    6
  5    2    4    6    8    3    1    7
  5    2    8    1    4    7    3    6
  5    3    1    6    8    2    4    7
  5    7    2    4    8    1    3    6
  5    7    2    6    3    1    4    8
  5    8    4    1    3    6    2    7
  6    3    1    8    5    2    4    7
  6    3    5    7    1    4    2    8
  6    3    5    8    1    4    2    7
  6    4    7    1    3    5    2    8


SQL

col a for 99
col b for 99
col c for 99
col d for 99
col e for 99
col f for 99
col g for 99
col h for 99

select a,b,c,d,e,f,g,h
  from (select RowNum as a from dict where RowNum <=8),
       (select RowNum as b from dict where RowNum <=8),
       (select RowNum as c from dict where RowNum <=8),
       (select RowNum as d from dict where RowNum <=8),
       (select RowNum as e from dict where RowNum <=8),
       (select RowNum as f from dict where RowNum <=8),
       (select RowNum as g from dict where RowNum <=8),
       (select RowNum as h from dict where RowNum <=8)
 where a not in(b,c,d,e,f,g,h)
   and b not in(  c,d,e,f,g,h)
   and c not in(    d,e,f,g,h)
   and d not in(      e,f,g,h)
   and e not in(        f,g,h)
   and f not in(          g,h)
   and g not in(            h)
   and abs(a-b) != 1 and abs(a-c) != 2 and abs(a-d) != 3 and abs(a-e) != 4 and abs(a-f) != 5
   and abs(a-g) != 6 and abs(a-h) != 7 and abs(b-c) != 1 and abs(b-d) != 2 and abs(b-e) != 3
   and abs(b-f) != 4 and abs(b-g) != 5 and abs(b-h) != 6 and abs(c-d) != 1 and abs(c-e) != 2
   and abs(c-f) != 3 and abs(c-g) != 4 and abs(c-h) != 5 and abs(d-e) != 1 and abs(d-f) != 2
   and abs(d-g) != 3 and abs(d-h) != 4 and abs(e-f) != 1 and abs(e-g) != 2 and abs(e-h) != 3
   and abs(f-g) != 1 and abs(f-h) != 2 and abs(g-h) != 1
   and a < h -- 線対称のパターンを排除
order by a,b,c,d,e,f,g,h;


解説

答えは、92通りですが、
線対称のパターンを排除して92/2=46通りの答えとなります。