Crazy Rows (Large)
Time limit5sMemory limit512 MB
Given a binary N x N matrix, swap adjacent rows to move every 1 to or below the main diagonal, and output the minimum number of swaps.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
You are given an matrix whose entries are 0 and 1. You may swap any two adjacent rows.
The goal is to place every 1 on or below the main diagonal. That is, for every with , row must contain no 1 to the right of column .
Find the minimum number of row swaps needed to reach the goal.
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains one integer . Each of the next lines contains characters. Each character is 0 or 1.
Limits
Output
For each test case, print one line in this format.
Case #X: K
Here is the test case number starting from 1, and is the minimum number of row swaps needed to place every 1 on or below the main diagonal.
Every test case is guaranteed to have a solution.