The Sultan's Successors
InterviewTime limit1sMemory limit128 MB
For each 8x8 board, place 8 non-attacking queens to maximize the sum of the numbers on the occupied squares.
- Level
Medium4 of 10
- Topics
- Backtracking, Recursion, Brute force, Implementation
- Solved
- No attempts yet
Problem
The Sultan of Nubia has no children, so she has decided that on her death the country will be split into up to separate parts, each inherited by whoever performs best on a test. One person may inherit more than one part, or even all of them.
To ensure that only highly intelligent people become her successors, the Sultan devised a test. In a large hall she places chessboards. Each chessboard is an grid with a number from to written on every square, and comes with jewelled chess queens. Each candidate must place the queens on a board so that no queen attacks another, and so that the numbers on the chosen squares sum to a value at least as high as one chosen in advance by the Sultan.
(In chess this means that every row and every column contains exactly one queen, and every diagonal contains at most one queen.)
Write a program that reads the chessboards and, for each board, determines the highest possible sum obtainable by placing the non-attacking queens. (The Sultan is both a strong chess player and a fine mathematician, so the score she chose is the best attainable.)
Input
The first line contains , the number of boards (). It is followed by boards. Each board is given as lines of integers, i.e. numbers in total, where every number is a positive integer less than .
Output
For each board, output one line containing its highest possible score. Each score is right-justified in a field characters wide.