Programming contests

ACM ICPC programozó csapatverseny, országos forduló, 2013. október 27.

October 27, 2013 10:00 AM – October 27, 2013 3:00 PM

Grid Game

Alice and Bob both have lots of candies but want more. They decide to play the following turn-based game.

They fill an n × n grid M with random integers. Alice begins the game by crossing off an uncrossed row i of the grid. Now it’s Bob turn and he crosses off an uncrossed column j of the grid. At the end of Bob’s turn, Alice takes the number candies in the ith row and jth column of M, call this value M(ij), from Bob. (If M(ij) is negative, then Alice gives |M(ij)| candies to Bob.) The game continues alternating turns from Alice to Bob until the entire board is crossed off.

What is the largest amount of candies that Alice can win from Bob (or least amount to lose if she cannot win) if both Alice and Bob play optimally?


Figure 1: The beginning of a game between Alice (red) and Bob (blue).

Input Specification

The first line of the input contains an integer t (1 ≤ t ≤ 20), the number of test cases. Each test case starts with n (1 ≤ n ≤ 8), the size of the grid. Then follow n lines containing n numbers separated by spaces describing M. We call the jth number on ith line M(ij) (-1000 ≤ M(ij) ≤ 1000).

Output Specification

For each test case, print the largest amount of candies that Alice can win from Bob. If she cannot win, print the negative number indicating the minimum number of candies she loses.

Sample Input

  1. 3
  2. 2
  3. 10 10
  4. -5 -5
  5. 2
  6. 10 -5
  7. 10 -5
  8. 2
  9. 10 -5
  10. -5 10
download as text file

Output for Sample Input

  1. 5
  2. 5
  3. -10
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019