12948 - Eight Queens Puzzle   

Description

The eight queens puzzle is the problem of placing eight chess queens on an 8 times 8 chessboard and no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.

 

One possible solution is shown as the figure below.

 

Given where you place the first chess queen in column 0.

Please write a C program to show all the possible solutions.

Input

One integer R.

Note:

  1. 7 >= R >= 0.

Output

Output should follow below format:

n.

c c c c c c c c

c c c c c c c c

c c c c c c c c

c c c c c c c c

c c c c c c c c

c c c c c c c c

c c c c c c c c

c c c c c c c c

Note:

  1. Need to have a return value('\n') at the end of your string.
  2. Character c is one of ‘-’ and ‘Q’.
  3. Need a ‘Space’ between two characters.
  4. n is the number index of solutions, which starts counting from 1.
  5. You should print out these solutions in an order based on the row number of the queen (defined as “queen location”) in the column 2. For any two solutions, a tie occurs when the queen locations in the same column are equal. To break the tie, you can sort the tied solutions based the queen locations in the next adjacent column (i.e., column 3). If the tie still exists when sorting based on the queen locations in the column i (i = 3~6), you can continue to pick the queen locations in the following column j (i.e., j=i+1) until the tie breaks.
 

Sample Input  Download

Sample Output  Download

Tags




Discuss