1. 


/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS09
* Problem: 750 - 8 Queens Chess Problem
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=9&page=show_problem&problem=691
*
* @author Kratzer Kevin
* @version 1.0, 10/28/2009
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.256
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main750 {

protected final BufferedReader input;
private int solNr = 1;
private int skipLine = 0;
public Main750() {
input = new BufferedReader(new InputStreamReader(System.in));

}

void process() throws IOException {
Scanner s = new Scanner(input);
int amount = s.nextInt();
for(int i = 0; i < amount; ) {
solNr = 1;
int[] queens = new int[8];
int col = s.nextInt() -1;
skipLine = s.nextInt() -1;
queens[skipLine] = col;
System.out.println("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n");
for(int newCol = 0; newCol < 8; newCol++) {
int[] newQueens = new int[8];
System.arraycopy(queens, 0, newQueens, 0, 8);
backtrack(newQueens,0,newCol);
}
i++;
if(i < amount) {
System.out.println();
}
}
System.out.flush();
}

private void backtrack(int[] queens, int line, int col) {
if(line == skipLine) {
line++;
}
if(line > 7) {
System.out.printf("%2d ",solNr);
solNr++;
for(int i = 0; i < 8;) {
System.out.print((queens[i]+1));
i++;
if(i < 8) {
System.out.print(" ");
} else {
System.out.print("\n");
}
}
return;
}
boolean valid = !(queens[skipLine] == col ||
(Math.abs(skipLine-line) == Math.abs(queens[skipLine]-col)));
for(int i = 0; i < line; i++) {
if(queens[i] == col ||
(Math.abs(i-line) == Math.abs(queens[i]-col))) {
valid = false;
}
}
if(valid) {
int[] newQueens = new int[8];
System.arraycopy(queens, 0, newQueens, 0, 8);
newQueens[line] = col;
if(line == 7 || skipLine == 7 && line == 6) {
backtrack(newQueens,8,-1);
} else {
for(int newCol = 0; newCol < 8; newCol++) {
backtrack(newQueens,line+1,newCol);
}
}
}
}



public static void main(String... args) throws Exception {
new Main750().process();
}
}

2.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS09
* Problem: 750 8 Queens Chess Problem
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=9&page=show_problem&problem=691
*
* @author Stefan Gohlke
* @version 1.0, 11/18/2009
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.276
*/

package eightQueens;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
eightQueens();
}

public static void eightQueens() {
Scanner scanner = new Scanner(System.in);

int anzahl = scanner.nextInt();

int[] rArray = new int[anzahl];
int[] cArray = new int[anzahl];

for (int i = 0; i < anzahl;i++)
{
rArray[i] = scanner.nextInt();
cArray[i] = scanner.nextInt();
}

for (int j = 0; j < anzahl;j++)
{
int c = rArray[j];
int r = cArray[j];

int nummerLoesung = 1;
int[] x = new int[8];

if (j != 0)
{
System.out.println();
System.out.println("\nSOLN COLUMN");
}
else
{
System.out.println("SOLN COLUMN");
}

System.out.println(" # 1 2 3 4 5 6 7 8");

int k = 0;
x[k] = -1;

while (k >= 0) {
boolean flag = false;
while (!flag && x[k] < 7) {
x[k]++;
flag = true;
for (int i = 0; i < k; i++) {
if (x[i] == x[k] || Math.abs(x[i] - x[k]) == (k - i))
flag = false;
}
}

if (flag) {
if (k == 7) {

if (x[r - 1] == c - 1) {
System.out.println();
System.out.printf("%2d", nummerLoesung);
System.out.print(" ");

for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + 1);
if (i != x.length - 1) System.out.print(' ');
}
nummerLoesung++;
}
} else
x[++k] = -1;
} else
k--;
}
}
System.out.println();
System.exit(0);
}
}