The Sultan's Successors UVA - 167

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Sultan's Successors UVA - 167相关的知识,希望对你有一定的参考价值。

?????????familiar   rmi   sample   row   ever   lib   ber   diagonal   each   

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

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int c[150],vis[3][150],tot,n=8,sum_max;
int mapn[9][9];
void search(int cur)
{
    if(cur==n)//??????????????????????????????????????????????????????????????????
    {
        if(sum_max<tot)
            sum_max = tot;
    }
    else for(int i=0;i<n;i++)
    {
        if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n])//??????????????????????????????
        {//0????????????1??????????????????2???????????????
            c[cur] = i;//??????????????????????????????
            tot += mapn[cur][i];
            vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 1;
            search(cur+1);
            vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 0;//???????????????
            tot -= mapn[cur][i];
        }
    }
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        sum_max = 0,tot = 0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<8;i++)
            for(int j=0;j<8;j++)
            {
                cin >> mapn[i][j];
            }
        search(0);
        printf("%5d\n",sum_max);
    }
    return 0;
}

 

以上是关于The Sultan's Successors UVA - 167的主要内容,如果未能解决你的问题,请参考以下文章

Sultan's Dowry Problem - 苏丹新娘问题

UVA11540 Sultan's Chandelier

What's the difference between all the Selection Segues?

The Frog's Games(二分)

The Frog's Games

Jenkins: because the document's frame is sandboxed and the 'allow-scripts' permission