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.
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:
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:
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.
Output for Sample Input
|University of Debrecen; Faculty of Informatics; v. 03/01/2019|