This page is still under construction.

Parts of this page are still being built. What you see may change.

Crazy Rows (Small)

Time limit5sMemory limit512 MB

Summary
Given an N by N binary matrix, reorder the rows using adjacent swaps so each row's rightmost 1 is at or left of its position, minimizing swaps.
Level

Medium5 of 10

Topics
Greedy, Sorting, Array, Brute force
Solved
No attempts yet

Problem

You are given an N×NN \times N matrix whose entries are 0 and 1. You can swap any two adjacent rows of the matrix.

Your goal is to have every 1 on the main diagonal or below it. That is, for each XX with 1≤X≤N1 \le X \le N, row XX must contain no 1 to the right of column XX.

Report the minimum number of row swaps needed to reach the goal.

Input

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

The first line of each test case contains one integer NN. Each of the next NN lines contains NN characters. Each character is either 0 or 1.

Limits

  • 1≤T≤601 \le T \le 60
  • 1≤N≤81 \le N \le 8

Output

For each test case, print one line in the form

Case #X: K

where XX is the test case number starting from 1, and KK is the minimum number of row swaps needed to have every 1 on the main diagonal or below it.

Every test case has a solution.

Examples3

  1. Example 1

    Input
    3
    2
    10
    11
    3
    001
    100
    010
    4
    1110
    1100
    1100
    1000
    
    Expected output
    Case #1: 0
    Case #2: 2
    Case #3: 4
    
  2. Example 2

    Input
    2
    1
    0
    1
    1
    
    Expected output
    Case #1: 0
    Case #2: 0
    
  3. Example 3

    Input
    3
    2
    11
    10
    2
    10
    01
    2
    00
    00
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: 0