# Programming contests

## ECN selejtező programozó csapatverseny, 2016. március 23.

March 23, 2016 10:10 AM – March 23, 2016 3:10 PM

# 8 Queens Chess Problem

In chess, it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will determine all such possible arrangements for eight queens, given the initial position of one of the queens.

Do not attempt to write a program which evaluates every possible configuration of one queen placed in each row (or column) of the board. This would require 88 evaluations and would bring the system to its knees. There will be a reasonable runtime constraint placed on your program.

## Input Specification

The first line of the input contains the number of test cases, T. Then T test cases follow, each on a line by itself. Each test case will be two numbers, separated by a blank. The numbers represent the square on which one of the eight queens must be positioned. A valid square will be represented; it will not be necessary to validate the input.

To standardize our notation, assume that the upper leftmost corner of the board is position (1,1). Rows run horizontally, and the top row is row 1. Columns are vertical, and column 1 is the leftmost column. Any reference to a square is by row then column; thus, square (4,6) means row 4, column 6:

## Output Specification

For each test case, the output should be as described below. Print a blank line between test cases — not after each test case. (So, there should not be a blank line printed after the last test case.)

The output for each test case will consist of a one-line-per-solution representation. Each solution will be sequentially numbered: 1, …, N. Each solution will consist of 8 numbers. Each of the 8 numbers will be the ROW coordinate for that solution. The column coordinate will be indicated by the order in which the 8 numbers are printed. That is, the first number represents the ROW in which the queen is positioned in column 1; the second number represents the ROW in which the queen is positioned in column 2, and so on.

The sample input below produces 4 solutions. The full 8 × 8 representation of each solution is shown below:

```   SOLUTION 1        SOLUTION 2        SOLUTION 3        SOLUTION 4 1 0 0 0 0 0 0 0   1 0 0 0 0 0 0 0   1 0 0 0 0 0 0 0   1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 1 0   0 0 0 0 0 1 0 0   0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0   0 0 0 1 0 0 0 0   0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1   0 0 0 0 0 1 0 0   0 0 1 0 0 0 0 0   0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0   0 0 0 0 0 0 0 1   0 0 0 0 0 0 1 0   0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0   0 1 0 0 0 0 0 0   0 0 0 1 0 0 0 0   0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0   0 0 0 0 1 0 0 0   0 1 0 0 0 0 0 0   0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0   0 0 1 0 0 0 0 0   0 0 0 0 1 0 0 0   0 0 0 1 0 0 0 0```

Do not submit the board matrices as part of your solution! Submit only the one-line, 8-digit representation of each solution as described earlier. Solution #1 below indicates that there is a queen at row 1, column 1; row 5, column 2; row 8, column 3; row 6, column 4; row 3, column 5; …; row 4, column 8.

Include the two lines of column headings as shown below in the sample output, and print the solutions in lexicographical order. In the headings, the # character is separated from the column numbers by 6 spaces.

## Sample Input

1. `1`
2. `1 1`
1. `SOLN       COLUMN`
2. ` #      1 2 3 4 5 6 7 8`
3. ` 1      1 5 8 6 3 7 2 4`
4. ` 2      1 6 8 3 7 4 2 5`
5. ` 3      1 7 4 6 8 2 5 3`
6. ` 4      1 7 5 8 2 4 6 3`