#P13440. [GCJ 2009 #2] Crazy Rows

[GCJ 2009 #2] Crazy Rows

题目描述

You are given an N×NN \times N matrix with 00 and 11 values. You can swap any two adjacent rows of the matrix.

Your goal is to have all the 11 values in the matrix below or on the main diagonal. That is, for each XX where 1XN1 \leq X \leq N, there must be no 11 values in row XX that are to the right of column XX.

Return the minimum number of row swaps you need to achieve the goal.

输入格式

The first line of input gives the number of cases, TT. TT test cases follow.

The first line of each test case has one integer, NN. Each of the next NN lines contains NN characters. Each character is either 00 or 11.

输出格式

For each test case, output

Case #X: K

where XX is the test case number, starting from 11, and KK is the minimum number of row swaps needed to have all the 11 values in the matrix below or on the main diagonal.

You are guaranteed that there is a solution for each test case.

3
2
10
11
3
001
100
010
4
1110
1100
1100
1000
Case #1: 0
Case #2: 2
Case #3: 4

提示

Limits

  • 1T601 \leq T \leq 60

Small dataset(6 Pts)

  • 1N81 \leq N \leq 8

Large dataset(10 Pts)

  • 1N401 \leq N \leq 40