UVA The Sultan's Successors

来源:互联网 发布:知微数据华为 编辑:程序博客网 时间:2024/06/07 23:49

题目如下:

The Sultan's Successors 

The Sultan of Nubia has no children, so she has decided that thecountry will be split into up tok separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any individual to inherit more than one or indeed allof the portions. To ensure that only highly intelligent peopleeventually become her successors, the Sultan has devised an ingenioustest. In a large hall filled with the splash of fountains and thedelicate scent of incense have been placedk chessboards. Eachchessboard has numbers in the range 1 to 99 written on each square andis supplied with 8 jewelled chess queens. The task facing eachpotential successor is to place the 8 queens on the chess board insuch a way that no queen threatens another one, and so that thenumbers on the squares thus selected sum to a number at least as highas one already chosen by the Sultan. (For those unfamiliar with therules of chess, this implies that each row and column of the boardcontains exactly one queen, and each diagonal contains no more thanone.)

Write a program that will read in the number and details of thechessboards and determine the highest scores possible for each boardunder these conditions. (You know that the Sultan is both a good chessplayer and a good mathematician and you suspect that her score is thebest 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 linesof eight numbers. Each number will be a positive integer less than100. There will never be more than 20 boards.

Output

Output will consist of k numbers consisting of your k scores, eachscore on a line by itself and right justified in a field 5 characterswide.

Sample input

1 1  2  3  4  5  6  7  8 9 10 11 12 13 14 15 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 4848 50 51 52 53 54 55 5657 58 59 60 61 62 63 64

Sample output

  260

经典的八皇后问题,白书上有详细讲解。这里不过是多了一个求所在格子数的和的最大值,只需求出一种情况后维护最大值即可。

AC的代码如下:


0 0