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. 四个漂亮CSS样式表

    1. 单像素的边框CSS表格 这是一个非常所用的表格风格. 源码: <!-- CSS goes in the document HEAD or added to your external st ...

  2. kernel 于ioctl申请书

    ioctl经营无纸装置频繁使用的类型.同时这是一个非常实用的方法进程调试. 这里正在进行wdt该文章继续 static long at91_wdt_ioctl(struct file *file, u ...

  3. git 常用命令及问题解决(转)

    git init 产生的目录解释error: src refspec master does not match any.引起该错误的原因是,目录中没有文件,空目录是不能提交上去的error: ins ...

  4. Java Swing 绝对布局管理方法,null布局(转)

    首先把相关容器的布局方式设为 setLayout(null); 然后调用组件的  setBounds() 方法 设置button的位置为(100,100) 长宽分别为 60,25 jButton.se ...

  5. windows下一个erlang包装镜像启动

    于linux环境,erlang经systools:make_script("",[])和systools:make_tar()命令生成图像包,安装镜像包,图片包的安装过程,通过替换 ...

  6. i++和i--运算符优先级

    1.问题背景 /** * 測试i++和i-- */ package com.you.model; /** * @author YouHaiDong * @date 2014-08-16 */ @Sup ...

  7. HTML5游戏开发实战--当心

    1.WebSocket它是HTML5该标准的一部分.Web页面可以用它来连接到持久socketserver在.该接口提供一个浏览器和server与事件驱动的连接.这意味着client每次需要时不再se ...

  8. 认识Backbone (三)

    Backbone.Collection(集合)  collection是model对象的一个有序的组合,我们可以在集合上绑定 "change" 事件,从而当集合中的模型发生变化时f ...

  9. myeclipse如何恢复已删除的文件和代码

    这是一篇文章分享秘诀:myeclipse恢复意外删除的文件和代码 [ 恢复误删文件 ] 今天在写代码的时候,不小心把一个包给删除了,然后这个包下全部的文件都没了,相信非常多人都有类似的经历. 幸好my ...

  10. Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies

    B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...