Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Perturbation theory wikipedia , lookup

Inverse problem wikipedia , lookup

Lateral computing wikipedia , lookup

Algorithm wikipedia , lookup

Mathematical optimization wikipedia , lookup

Pattern recognition wikipedia , lookup

Genetic algorithm wikipedia , lookup

Exact cover wikipedia , lookup

Simplex algorithm wikipedia , lookup

Knapsack problem wikipedia , lookup

Travelling salesman problem wikipedia , lookup

Multiple-criteria decision analysis wikipedia , lookup

Computational complexity theory wikipedia , lookup

Halting problem wikipedia , lookup

Weber problem wikipedia , lookup

Transcript
Second International Programming Contest
March 28, 2004, Blagoevgrad, Bulgaria
Problem B
The 88 Queens problem
The 8 Queens problem is well known and often used to demonstrate different programming techniques. It was
first analyzed in 1850 by Gauss, who defined it as trying to situate eight queens on a standard chessboard 8x8
in a way so that no one threatens another one, as defined by the rules of chess.
Here we will consider a slight deviation from the original problem: the “8 8 Queens problem”. The rules are the
same as in the original one, except that the board is much bigger: up to 8 8 x88.
Your task will be to implement an algorithm that can solve the problem for any board of size up to 8 8. However,
to check the correctness we will only want to make sure your implementation works for N < 10000. If so, we will
assume the algorithm should in principle be practical on bigger boards as well.
Given a square board of size NxN (5 N < 10000) the goal is to put N queens so that there are no two ones on
the same horizontal, vertical or diagonal. A sample solution for N = 8 is shown on the picture below.
.....
Input
The first line contains the number of tests. Each test is described by a single line: the number N.
Output
For each test case you should output exactly one line. If there is no solution it should say “NO SOLUTION”.
Otherwise, the line should contain N integers R1 R2 … RN separated by a single space, where Ri is the column
number of the queen in row i (1 <= i <= N).
Example
Sample Input file
2
5
8
Sample Output file
1 3 5 2 4
2 7 3 6 8 5 1 4