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. Java Web整合开发(80) -- EJB & WebService

    1. jdk-6u18-windows-i586-p.execlasspath: .;%JAVA_HOME%lib/tools.jar;%JAVA_HOME%lib/dt.jar;%JAVA_HOME ...

  2. Lua 服务器Socket通信实例(转)

    local socket = require"socket" local host = "127.0.0.1"local port = "843&qu ...

  3. gif动图快速制作方法(附工具)(转)

    现在写博客或是wiki的过程中,会经常引用到图片,特别是客户端经常与页面相关所以截图不可避.但是越来越多的效果仅仅一张图片是无法清楚的描述.并且博客或是wiki也是支持gif图的.gif图的制作方法有 ...

  4. Spring3.2 HelloWorld

    直接上图吧: jar包: 项目文件夹一览: 这里的HelloWeb-servlet,xml 是在WEB-INF 下 HelloController: package com.cqu.tutorial; ...

  5. 怎么样cocos2d-x正在使用ECS(实体-包裹-制)建筑方法来开发一款游戏?

    简介 在我的博客,我翻译的几篇文章ECS文章.这些文章都是从Game Development站点.假设你对这个架构方式还不是非常了解的话.欢迎阅读理解 组件-实体-系统和实现 组件-实体-系统. 我发 ...

  6. SQL server 2005 PIVOT运算符的使用

    原文:SQL server 2005 PIVOT运算符的使用 PIVOT,UNPIVOT运算符是SQL server 2005支持的新功能之一,主要用来实现行到列的转换.本文主要介绍PIVOT运算符的 ...

  7. CFileDialog 使用简单介绍

    CFileDialog使用文件选择对话框:首先构造一个对象,并提供一个相应的参数,构造函数原型例如,下面的: CFileDialog::CFileDialog( BOOL bOpenFileDialo ...

  8. Java的λ表达(lambda)

    λ表达的基本目的 回调Java8的λ表达式 说明了Java8的λ表达式的基本用途:完毕了回调的原意--代码的參数化. 回调:能够简单地说,假设你的方法须要override底层或JDK的某个类的方法,并 ...

  9. [Ext JS 4] 实战Chart 协调控制(单一的坐标,两个坐标)

    前言

  10. javascript事件和事件处理

    于js期间事件处理被分成三个步骤: 1.发生事件 2.启动事件处理程序 3.事件处理程序做出反应 事件处理程序的调用 1.在javascript中 在javascript中调用事件处理程序,首先要获得 ...