# 8 Queens Chess Problem

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 would require 88evaluations and would 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 of 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 4

1 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 0
0 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 0
0 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 1
0 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 0
0 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 0
0 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 0
0 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 0
0 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

1

1 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*, 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%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;
}
}

• 本文已收录于以下专栏：

## POJ1321-Chess Problem

• 2011年07月31日 22:46
• 6KB
• 下载

## 700 chess problem

• 2010年04月30日 10:38
• 705KB
• 下载

## Google中国2015校园招聘笔试Round D APAC Test Problem D. Itz Chess

Problem Given an arranged chess board with pieces, figure out the total number of different ways in...

CF 313 DIV2 E

## Hihocoder 1424 Asa's Chess Problem （有源汇上下界最小费用流）

ProblemAsa comes up with a chess problem. There are N×N chesses on a board with N×N grids, one chess...

## Sicily 1276. Who needs 8 Queens w

1276. Who needs 8 Queens w Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Jud...

举报原因： 您举报文章：8 Queens Chess Problem 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)