Title Description
People play chess very clear: the queen can not eat another piece to the number of steps in the horizontal, vertical, diagonal. How eight queens on the chessboard (8 * 8 squares), so that they can not be eaten by anyone! This is the famous eight queens problem.
8. A method for placement of a queen to meet the requirements of the definition of a corresponding sequence a queen, i.e. a = b1b2 ... b8, where bi is the corresponding number of columns in row i Pendulum Queen located. 8 has been known a total of 92 Queens set of solutions (i.e., 92 different strings Queen).
A given number b, b of the required output strings. String comparison is such that: before Queen Queen disposed string string x y, y if and only if x is smaller than when considered integer.
Entry
Line 1 is the set of n number of test data, followed by n input lines. Each set of test data representing a line, comprising a positive integer b (1 <= b <= 92)
Export
There are n output lines, each output line corresponds to an input. The output should be a positive integer, b is the queen of the corresponding string.
Sample input
3
6
4
25
Sample Output
25,713,864
17,582,463
36,824,175
mainly for processing the diagonal,
the left diagonal [i + n] [jn] ;
right diagonal of [i + n] [j +
n]; Note that determining whether bounds.
#include <the iostream>
#include <cstdio>
the using namespace STD;
int n-, Q [. 9], G [. 9] [. 9] // marked;
String STR [93];
void DFS (int depth) {
IF (depth . 9 ==) {
++ Z;
for (int. 1 = A; A <=. 8; A ++) {
STR [Z] + = (char) (Q [A] +48);
}
return;
}
for (int I =. 1; I <=. 8; I ++) {
IF (! (G [depth] [I])) {
for (int J =. 1; J <=. 8; J ++) {
G [depth] [J] + =. 1 ; G [J] [I] + =. 1; // for column labeled IF ((depth + J) <=. 8 && (I + J) <=. 8) G [depth + J] [I + J] + =. 1 ; // for right diagonal mark if ((depth + j <= 8) && (ij)> 0)
g [depth + j] [ij ] + = 1; // A left diagonal mark
}
Q [depth] = I;
DFS (+ depth. 1);
for (int. 1 = J; J <=. 8; J ++) {
G [depth] [J] - =. 1;
G [J] [I] - =. 1;
IF ((depth + J) <=. 8 && (I + J) <=. 8)
G [depth + J] [I + J] - =. 1;
IF ((depth + J <=. 8) && (ij of)> 0)
G [depth + J] [ij of] - =. 1;
}
}
the else Continue;
}
return;
}
int main () {
int Z;
CIN n->>;
DFS (. 1);
for (int I = 0; I <n-; I ++) {
CIN >> Z;
COUT << STR [Z] << endl;
}
return 0;
}
I white papers first solution to a problem, many have are not very good, hope forgive. I will continue to work hard ^ ~ ^.