Problem

You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix.

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

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

Input

The first line of input gives the number of cases, TT test cases follow.
The first line of each test case has one integer, N. Each of the next N lines contains Ncharacters. Each character is either 0 or 1.

Output

For each test case, output

Case #X: K

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

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

Limits

1 ≤ T ≤ 60

Small dataset

1 ≤ N ≤ 8

Large dataset

1 ≤ N ≤ 40

Sample

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

代码:

 int n;
int mp[MAX][MAX]; //矩阵 int a[MAX]; //表示第i行最后出现1的位置 void solve()
{
int ans=;
for(int i=; i<n; i++){
a[i]=-;
for(int j=; j<n; j++){
if(mp[i][j]==)
a[i]=j;
}
}
for(int i=; i<n; i++){
int pos=-;
for(int j=i; j<n; j++){
if(a[j]<=i){
pos=j;
break;
}
} for(int j=pos; j>i; j--){
swap(a[j],a[j-i]);
ans++;
}
}
printf("%d",&ans);
}

Crazy Rows的更多相关文章

  1. 2009 Round2 A Crazy Rows (模拟)

    Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...

  2. GCJ——Crazy Rows (2009 Round 2 A)

    题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...

  3. GCJ 2009 Round 2 Problem A. Crazy Rows

    https://code.google.com/codejam/contest/204113/dashboard 题目大意: 给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数 ...

  4. 【刷题记录】GCJ 2.71~2.72

    GCJ 271 [题目大意] Minimum Scalar Product 有两个东西(滑稽)v1=(x1,x2,x3,……,xn)和v2=(y1,y2,……yn),允许任意交换v1和v2中各数字的顺 ...

  5. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  6. Light OJ 1393 Crazy Calendar (尼姆博弈)

    C - Crazy Calendar Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Su ...

  7. Crazy Calendar (阶梯博弈变形)

    2011 was a crazy year. Many people all over the world proposed on 11-11-11, married on 11-11-11, som ...

  8. DevOps is dirty work - CI drives you crazy

    一直很想谈谈Continuous Integration(CI),持续集成. 就在不久前一次朋友聚会上,一个刚刚跳槽到一家创业公司的朋友跟我抱怨说他们没有CI,没有code review,要做点事太累 ...

  9. Here's to the crazy ones.

    Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square h ...

随机推荐

  1. Oracle db中 CONNECT role的含义

    出自: Complete Checklist for Manual Upgrades to 11gR2 (文档 ID 837570.1) 该文章中有例如以下的文字: From Oracle 10.2, ...

  2. COJ 1102 - You Can Say 11 题解

    本题就是给出一个无穷大数,算其能否被11除尽 Description Your job is, given a positive number N, determine if it is a mult ...

  3. mysql监视器MONyog的使用

    MONyog是个商业收费软件,可是能够找一下破解版.我用的是4.72破解版 1.       图1.1 在server设置中,如图1.1. 在Sniffer Settings里Enable sniff ...

  4. Directx11学习笔记【十三】 实现一个简单地形

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5510294.html 上一个教程我们实现了渲染一个会旋转的立方体, ...

  5. 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...

  6. iOS 辛格尔顿

    单例模式: 为什么使用单例,单例模式的用途是什么?以下我们举一个样例来诠释一下 举个大家都熟知的样例--Windows任务管理器,如图,我们能够做一个这种尝试,在Windows的"任务栏&q ...

  7. eclipse在maven项目交付svn忽略简介

    文章来源:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明. 不时因为它将有关鲍恩梳理,它会做出相应的内容不变.文. ecl ...

  8. js防堵塞载入

    js防堵塞载入 <script type="text/javascript"> function scriptDomElement (u) { var s = docu ...

  9. 开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解

    原文:[原创]开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math.NET(一) ...

  10. 开销是有益的:AppCan 至HTML5移动创新和创业精神和健康

      2014年移动创业更趋向理性,消费级App市场接近饱和,BAT等巨头的竞争更加激烈,市场版图及格局基本定型.而企业级移动应用却迎来爆发增长,替代进入红海的消费级App市场,企业级定制APP开发成为 ...