1. Java, Andreas Kunft

/*

 * ACM Programming Contest

 * Problem:     750 (8 Queens Chess Problem)

 * Status:        Accepted

 * Language:    JAVA

 * Runtime:        0.350

 * Date:        2009-04-22 11:55:06

 * Author:        Andreas Kunft

 */



import java.io.IOException;

import java.util.Scanner;



public class Main

{

    public final static int SIZE = 8;



    public static void main(String... args) throws IOException

    {

        Scanner in = new Scanner(System.in);

        try

        {

            int times = in.nextInt();

            for (int cycle = 0; cycle < times; cycle++)

            {

                int row = in.nextInt() - 1;

                int col = in.nextInt() - 1;



                if (cycle > 0)

                    System.out.println();

               

                System.out.println("SOLN       COLUMN");

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

               

                int[] COLUMNS = new int[SIZE];

               

                int currentColumn = 0;

                int countSolutions = 0;

                COLUMNS[currentColumn] = -1;

                while (currentColumn >= 0)

                {

                    boolean flag = false;

                    while (!flag && COLUMNS[currentColumn] < SIZE - 1)

                    {

                        COLUMNS[currentColumn]++;

                        flag = true;

                        for (int i = 0; i < currentColumn; i++)

                            if (COLUMNS[i] ==
COLUMNS[currentColumn] || Math.abs(COLUMNS[i] - COLUMNS[currentColumn])
== currentColumn - i)

                                flag = false;

                    }

                    if (flag)

                    {

                        if (currentColumn == SIZE - 1)

                        {

                            // solution

                            if (COLUMNS[col] == row)

                            {

                                countSolutions++;

                               

                                if ((cycle < times || times == 1))

                                    System.out.println();



                                if (countSolutions < 10)

                                    System.out.print(" " + countSolutions + "      ");

                                else

                                    System.out.print(countSolutions + "      ");



                                for (int i = 0; i < SIZE; i++)

                                {

                                    System.out.print(COLUMNS[i] + 1);

                                    if (i < SIZE - 1)

                                        System.out.print(' ');

                                }

                            }

                        }

                        else

                            COLUMNS[++currentColumn] = -1;

                    }

                    else

                        currentColumn--;

                }

                System.out.println();

            }

        }

        finally

        {

            in.close();

        }

    }

}

2.

/*

* ACM Programming Contest

* Problem: 750 - 8 Queens Chess Problem

* Status: Accepted

* Language: C++

* Runtime: 0.030

* Date: 2009-04-29 13:07:00

* Author: Axel Fiedler

*/
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define SIZE 8

int main()
{
int row, column, count, solution, pos;
bool flag;

int x[SIZE];

scanf("%d", &count);
for (int i = 0; i < count; i++)
{
scanf("%d %d", &row, &column);
row--;
column--;

solution = 0;
pos = 0;

memset(x, 0, SIZE);
x[pos] = -1;

if (i > 0)
printf("\n");

printf("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n");

while (pos >= 0)
{
flag = false;
while (!flag && (x[pos] < (SIZE - 1)))
{
x[pos]++;
flag = true;

for (int j = 0; j < pos; j++)
if ((x[j] == x[pos]) || (abs(x[j] - x[pos]) == (pos - j)))
flag = false;
}

if (flag)
{
if (pos == (SIZE - 1))
{
if (x[column] == row)
{
solution++;

if (solution < 10)
printf(" %d ", solution);
else
printf("%d ", solution);

for (int j = 0; j < SIZE; j++)
{
printf("%d", x[j] + 1);

if (j < (SIZE - 1))
printf(" ");
}

printf("\n");
}
}
else
x[++pos] = -1;
}
else
pos--;
}
}

return 0;
}