U E D R , A S I H C RSS

Eight Queen Problem/안정원

Difference between r1.9 and the current

@@ -5,6 +5,7 @@
# End Time : 2014. 03. 16. 1:24

{{{
~Python
from itertools import permutations
n = 8
cols = range(n)
@@ -22,6 +23,7 @@

= EightQueenProblem2 =
{{{
~Python
from itertools import permutations
n = 8
cols = range(n)
@@ -29,3 +31,6 @@
if n == len(set(vec[i]+i for i in cols)) == len(set(vec[i]-i for i in cols)):
print vec
}}}
 
----
[EightQueenProblem], [EightQueenProblem2], [안정원]



다른 언어라면 이렇게 쉽게 안되었을 문제인데, 역시 Python입니다 ^ 오 ^

EightQueenProblem

# Start Time : 2014. 03. 16. 1:08
# End Time : 2014. 03. 16. 1:24

~Python
from itertools import permutations
n = 8
cols = range(n)
for vec in permutations(cols):
  if n == len(set(vec[i]+i for i in cols)) == len(set(vec[i]-i for i in cols)):
    for i in cols:
      for j in cols:
        if vec[i] == j: print ('Q'),
        else: print '#',
      print ''
    print ''
    break


EightQueenProblem2

~Python
from itertools import permutations
n = 8
cols = range(n)
for vec in permutations(cols):
  if n == len(set(vec[i]+i for i in cols)) == len(set(vec[i]-i for i in cols)):
    print vec
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:11
Processing time 0.0344 sec