Crazy Rows (Small)
Time limit5sMemory limit512 MB
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 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 with , row must contain no 1 to the right of column .
Report 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 either 0 or 1.
Limits
Output
For each test case, print one line in the form
Case #X: K
where is the test case number starting from 1, and 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.