Queens (3) P38211


Statement
 

pdf   zip

html

Write a program to print the lexicographically smallest way to place n queens on an n × n chessboard so that no queen threatens another queen.

Input

Input consists of a natural number n > 0.

Output

Mark the queens with a ‘Q’, and the empty positions with a dot. Print the lexicographically smallest way (by rows, from top to bottom, and assuming that a ‘Q’ is smaller than a dot) to place n queens on an n × n chessboard so that no queen threatens another queen. If there is no solution, print “NO SOLUTION”.

Public test cases
  • Input

    20
    

    Output

    Q...................
    ..Q.................
    ....Q...............
    .Q..................
    ...Q................
    ............Q.......
    ..............Q.....
    ...........Q........
    .................Q..
    ...................Q
    ................Q...
    ........Q...........
    ...............Q....
    ..................Q.
    .......Q............
    .........Q..........
    ......Q.............
    .............Q......
    .....Q..............
    ..........Q.........
    
  • Input

    3
    

    Output

    NO SOLUTION
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C C++