[UVA] 750 - 8 Queens Chess Problem@Morris' Blog|PChome Online 人新台
2012-05-13 15:27:30| 人1,300| 回0 | 上一篇 | 下一篇

[UVA] 750 - 8 Queens Chess Problem

0 收藏 0 0 站台


 8 Queens Chess Problem 

In chess it is possible to place eight queens on the board so that no onequeen can be taken by any other. Write a program that will determine allsuch possible arrangements for eight queens given the initial position of oneof the queens.

Do not attempt to write a program which evaluates every possible 8configuration of 8 queens placed on the board. This would require 88evaluations and would bring the system to its knees. There will be areasonable 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 datasetwill be two numbers separated by a blank. The numbers represent the squareon which one of the eight queens must be positioned. A valid square will berepresented; it will not be necessary to validate the input.

To standardize our notation, assume that the upper left-most corner of theboard 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 toa 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 $1 dots N$.Each solution willconsist of 8 numbers. Each of the 8 numbers will be the ROW coordinate forthat solution. The column coordinate will be indicated by the order in whichthe 8 numbers are printed. That is, the first number represents the ROW inwhich the queen is positioned in column 1; the second number represents theROW in which the queen is positioned in column 2, and so on.

The sample input below produces 4 solutions. The full 8$times$8 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 41 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 00 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 00 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 10 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 00 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 00 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 00 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 00 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 describedearlier. 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 

11 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 <stdio.h>
#include <stdlib.h>
int x[8], y[8], used[8] = {};
int ax, ay, time;
int check(int a, int b, int idx) {
    int i;
    for(i = 0; i < idx; i++)
        if(abs(x[i]-a) == abs(y[i]-b))
            return 0;
    return 1;
}
void dfs(int idx) {
    if(idx == 8) {
        if(y[ay-1] == ax-1) {
            printf("%2d     ", ++time);
            int i;
            for(i = 0; i < 8; i++)
                printf(" %d", y[i]+1);
            puts("");
        }
        return;
    }
    int i;
    for(i = 0; i < 8; i++) {
        if(used[i] == 0 && check(idx, i, idx) != 0) {
            used[i] = 1;
            x[idx] = idx, y[idx] = i;
            dfs(idx+1);
            used[i] = 0;
        }
    }
}
int main() {
    int t, i, j, first = 0;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &ax, &ay);
        if(first)
            puts("");
        first = 1;
        puts("SOLN       COLUMN");
        puts(" #      1 2 3 4 5 6 7 8\n");
        time = 0;
        dfs(0);
    }
    return 0;
}

台: Morris
人(1,300) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][二分] 11045 - My T-shirt suits me
此分上一篇:[UVA][dfs] 167 - The Sultan's Successors

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86