码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-26 17:04:34      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

 The Sultan‘s Successors 

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. 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的矩阵,每个位置都对应着一个1到99的数,在满足的八皇后规则的前提下找到最后的总和最大的那一个,输出最大的值maxx
       八皇后的规则就是在一个8*8的棋盘中放八个棋子,并且这些棋子满足两两不在同一行,同一列,同一对角线上。


代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int map[9][9];
int v[9];
int maxx;
int q[9];

int findx(int cnt,int ii)
{
    for(int i=0;i<cnt;i++)
    {
        if(q[i] == ii|| cnt-i==q[i]-ii || cnt-i== ii-q[i])
        {
            return 0;
        }
    }
    return 1;
}

void DFS(int cnt,int sum)
{
    if(cnt == 8)
    {
        if(maxx < sum)
        {
            maxx = sum;
        }
        return ;
    }
    for(int i=0;i<8;i++)
    {
        if(v[i] == 0 && findx(cnt,i) == 1)
        {
            v[i] = 1;
            q[cnt] = i;
            DFS(cnt+1,sum+map[cnt][i]);
            v[i] = 0;
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        maxx = 0;
        memset(v,0,sizeof(v));
        DFS(0,0);
        printf("%5d\n",maxx);
    }
    return 0;
}


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

标签:

原文地址:http://blog.csdn.net/yeguxin/article/details/43154271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!