UVA-167The Sultan's Successors(八皇后问题)

Description

The Sultan of Nubia has no children, so she has decided that the country will be split into up tok 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 placedk 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 byk sets of 64 numbers, each set consisting of eight lines of eight numbers. Each number will be a positive integer less than 100. There will never be more than 20 boards.

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
     大致题意如下:一个人,没有孩子,要在死前分割财产,然后出了一个题,让人们做,也就是八皇后,8 * 8 的棋盘,棋盘有64个值,不同的放置方案有不同的和,求最大的和就可得出答案。
     八皇后问题的搜索算法,算是重温小白书的算法,一行一行的放,先放第一行,然后找可以放得列数,判断是否会攻击的时候,用到的公式非常巧妙!!
0 1 2 3 4 5 6 7
-1 0 1 2 3 4 5 6
-2 -1 0 1 2 3 4 5
-3 -2 -1 0 1 2 3 4
-4 -3 -2 -1 0 1 2 3
-5 -4 -3 -2 -1 0 1 2
-6 -5 -4 -3 -2 -1 0 1
-7 -6 -5 -4 -3 -2 -1 0
上个表格是主对角线的值也就是用列数减去行数得到。(副对角线列数加上行数)
贴上代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;

//C[i]数组存取的值皇后为第i行的列数;
int Map[9][9], Max, C[9];

void Eightqueen(int row, int sum)
{
    if(row > 8)
    {
        if(sum > Max)
            Max = sum;
        return ;
    }
    else
    {
        for(int column = 1; column <= 8; column++)
        {
            C[row] = column;
            int ok = 1;
            for(int j = 1; j < row; j++)
            {
                //判断是否在同一列,统一主对角线, 同一副对角线,上。
                //row - C[row] == j - C[j] 行数减列数就是副对角线的值。相同,列数加行数是主对角线的值
                if(C[row] == C[j] || row - C[row] == j - C[j] || row + C[row] == j + C[j])
                {
                    ok = 0;
                    break;
                }
            }
            if(ok)
            {
                Eightqueen(row + 1, sum + Map[row][column]);
            }
        }
    }

}

int main()
{
    int n, i, j;
    scanf("%d",&n);
    while(n--)
    {
        for(i = 1; i <= 8; i++)
            for(j = 1; j <= 8; j++)
            {
                scanf("%d",&Map[i][j]);
            }
        memset(C, 0, sizeof(C));
        Max = 0;
        Eightqueen(1, 0);
        printf("%5d\n",Max);
    }
    return 0;
}



你可能感兴趣的:(UVA-167The Sultan's Successors(八皇后问题))