167The Sultan's Successors

来源:互联网 发布:淘宝新品上架2小时内刷 编辑:程序博客网 时间:2024/05/22 03:45

原题:
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的棋盘,每个格上面有一个不超过99的数,现在问你按照八皇后问题的套路在棋盘上放棋子,把所有放棋子的格子上的数加起来,和最大是多少?输出结果长度为5

暴力回溯解法

#include <bits/stdc++.h>using namespace std;int k;int num[10][10],sum=0,ans;int x[10];bool judge(int pre,int col){    return x[pre]==x[col]||abs(x[pre]-x[col])==abs(pre-col);//判断是否在同一列和是否在对角线上}void dfs(int row){    if(row==9)    {        ans=max(ans,sum);        return;    }    else    {        int i,j;        for(i=1;i<=8;i++)        {            x[row]=i;            int flag=1;            for(j=1;j<row;j++)            {                if(judge(row,j))                {                    flag=0;                    break;                }            }            if(flag)            {                sum+=num[row][i];                dfs(row+1);                sum-=num[row][i];            }        }    }}int main(){    ios::sync_with_stdio(false);    cin>>k;    while(k--)    {        sum=ans=0;        memset(num,0,sizeof(num));        for(int i=1;i<=8;i++)        {            for(int j=1;j<=8;j++)                cin>>num[i][j];        }        dfs(1);        cout<<setw(5)<<ans<<endl;    }    return 0;}

解答:
就是普通的八皇后问题,判断列和对角线是否冲突即可,判断列是否冲突用一个数组x[]来标记每一行放的列数。判断对角线是否冲突用以前行放置的棋子的坐标与当前放置的坐标,对应的横纵坐标差的绝对值是否相等即可

0 0
原创粉丝点击