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 8 configuration of 8 queens placed on the board. This wocould require 88 evaluations and wocould bring the system to its knees. There will be
A reasonable run time constraint placed on your program.
Input
The first line of the input contains the number of datasets, and it's followed by a blank line. Each dataset 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 left-most 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 left-most column. any reference
To a square is by row then column; thus square (4, 6) means Row 4, column 6.
Each dataset is separated by a blank line.
Output
Output for each dataset will consist of a one-line-per-solution representation.
Each solution will be sequentially numbered. Each solution will consist
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 88 representation of each
Solution is shown below.
Do not submit the Board matrices as part of your solution!
SOLUTION 1 SOLUTION 2 SOLUTION 3 SOLUTION 41 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 00 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 00 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 10 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 00 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 00 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 00 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 00 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
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.
Print a blank line between datasets.
Sample Input
11 1
Sample output
SOLN COLUMN # 1 2 3 4 5 6 7 8 1 1 5 8 6 3 7 2 4 2 1 6 8 3 7 4 2 5 3 1 7 4 6 8 2 5 3 4 1 7 5 8 2 4 6 3
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Int N;
Void backtracking (INT, int *, int *);
Int main ()
{
Int N;
Int row, Col, array [8];
Bool blank_line;
While (scanf ("% d", & N )! = EOF)
{
Blank_line = 0;
For (INT I = 0; I <n; I ++)
{
Scanf ("% d", & Row, & col );
Row --, Col --;
If (blank_line) printf ("\ n ");
Printf ("soln column \ n ");
Printf ("#1 2 3 4 5 6 7 8 \ n ");
Printf ("\ n ");
Blank_line = 1;
Int rowput [8] = {0}, colput [8] = {0}, leftslash [15] = {0}, rightslash [15] = {0 };
Rowput [row] = 1;
Colput [col] = 1;
Leftslash [row + Col] = 1;
Rightslash [row-Col + 7] = 1;
N = 0;
Array [col] = row;
Backtracking (0, array, rowput, colput, leftslash, rightslash );
}
}
Return 0;
}
Void backtracking (int I, int array [], int rowput [], int colput [], int leftslash [], int rightslash [])
{
If (I = 8)
{
Printf ("% 2D", ++ N );
For (Int J = 0; j <8; j ++)
{
If (j) printf ("");
Printf ("% d", array [J] + 1 );
}
Printf ("\ n ");
Return;
}
If (colput [I])
{
Backtracking (I + 1, array, rowput, colput, leftslash, rightslash );
Return;
}
For (Int J = 0; j <8; j ++)
{
If (rowput [J] | leftslash [J + I] | rightslash [J-I + 7])
Continue;
Rowput [J] = 1;
Leftslash [I + J] = 1;
Rightslash [J-I + 7] = 1;
Array [I] = J;
Backtracking (I + 1, array, rowput, colput, leftslash, rightslash );
Rowput [J] = 0;
Leftslash [I + J] = 0;
Rightslash [J-I + 7] = 0;
}
}