3.4/3.5 The Sultan's Successors

HDOJ 1642

Problem Description
The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate parts on her death and each part will be inherited by whoever performs best at some test. It is possible for any individual to inherit more than one or indeed all of the portions. To ensure that only highly intelligent people eventually become her successors, the Sultan has devised an ingenious test. In a large hall filled with the splash of fountains and the delicate scent of incense have been placed k chessboards. Each chessboard has numbers in the range 1 to 99 written on each square and is supplied with 8 jewelled chess queens. The task facing each potential successor is to place the 8 queens on the chess board in such a way that no queen threatens another one, and so that the numbers on the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the board contains exactly one queen, and each diagonal contains no more than one.)

Write a program that will read in the number and details of the chessboards and determine the highest scores possible for each board under these conditions. (You know that the Sultan is both a good chess player and a good mathematician and you suspect that her score is the best attainable.)
 

Input
Input will consist of k (the number of boards), on a line by itself, followed by k sets of 64 numbers, each set consisting of eight lines of eight numbers. Each number will be a positive integer less than 100.
 

Output
Output will consist of k numbers consisting of your k scores, each score on a line by itself and right justified in a field 5 characters wide.
 

Sample Input
 
 
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 

Sample Output
 
 
260

八皇后问题 一直在hdoj上无法通过 都是 超时 ,首先是自己尝试写了一遍,用flag数组标记是否可以占据位置,但就是超时了。

然后就看着视频做,用row[8],ld[15],rd[15]三个数组来标记列,左对角线,右对角线,但还是超时;

然后百度搜索,用经典的递归算法,超时;

最后用位运算,才算通过。

#include <stdio.h>
//值的初始化问题
int num[8][8];
int x[8];
int count;
int upperlim;
void findEight(int row, int ld, int rd, int i);
int main() {
    int k;
    scanf("%d", &k);

    while (k-->0) {
        upperlim = 1;
        int i, j;
        for (i = 0; i<8; i++) {
            for (j = 0; j<8; j++) {
                scanf("%d", &num[i][j]);
            }
        }
        count=0;//一定要初始化
        upperlim = (upperlim << 8) - 1;//8皇后得到8列1
        findEight(0, 0, 0, 0);
        printf("%5d\n", count);
    }

}
void findEight(int row, int ld, int rd, int q) {
    if (row == upperlim) {
        int sum = 0;
        int i = 0;
        for (i = 0; i<8; i++)
            sum += num[i][x[i]];
        if (sum>count)
            count = sum;
        return ;
    }
    int d = upperlim&(~(row | ld | rd));//为1的为可用的位置
    while (d) {
        int pos = d&-d;//得出d中最右边为1的位置
        int flag = pos;
        int k = 0;
        while (flag != 0) {
            k++;
            flag=flag >> 1;
        }

        x[q] = (8 - k);
        d -= pos;//最右边的位置1被使用变为0
        findEight(row | pos, (ld|pos) << 1, (rd | pos) >> 1, q + 1);//列 、左对角线、右对角线

    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值