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 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

代码:

 1 int n;
 2 int mp[MAX][MAX];  //矩阵
 3
 4 int a[MAX];   //表示第i行最后出现1的位置
 5
 6 void solve()
 7 {
 8     int ans=0;
 9     for(int i=0; i<n; i++){
10         a[i]=-1;
11         for(int j=0; j<n; j++){
12             if(mp[i][j]==1)
13                 a[i]=j;
14         }
15     }
16     for(int i=0; i<n; i++){
17         int pos=-1;
18         for(int j=i; j<n; j++){
19             if(a[j]<=i){
20                 pos=j;
21                 break;
22             }
23         }
24
25         for(int j=pos; j>i; j--){
26             swap(a[j],a[j-i]);
27             ans++;
28         }
29     }
30     printf("%d",&ans);
31 }

时间: 2024-10-20 09:34:09

Crazy Rows的相关文章

GCJ——Crazy Rows (2009 Round 2 A)

题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: 假如每一行的1的个数都是不相同的,即,最终答案中的矩阵是唯一的,这就相当于求对给定数组冒泡排序需要几次交换一样.但显然,题目没有如此保证. 方法是贪心法:(策略不给出证明) 从第一行到最后一行依次满足,因为可以满足前面行的也一定可以满足后面的,所以每次只需要找到可以满足当前行的最近的就可以了. 预处理最后

每天水一水 Crazy Rows (2009 Round2 A) GCJ

#include <bits/stdc++.h> using namespace std; #define maxn 10000 + 10 int M[maxn][maxn]; int mark[maxn]; int n, m; int solve() { int ans = 0; for(int i=1; i<=n; i++) { int pos = -1; for(int j=i; j<=n; j++) { if(mark[j] <= i) { pos = j; brea

Light OJ 1393 Crazy Calendar (尼姆博弈)

C - Crazy Calendar Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1393 Description 2011 was a crazy year. Many people all over the world proposed on 11-11-11, married on 11-11-11, some even w

HDOJ 5325 Crazy Bobo 树形DP

按照升序或者降序选择的点集可以满足条件..... 树上的每个节点可以从子节点转移,也可以从父亲节点转移 Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 100 Problem Description Bobo has a tree,whose vert

Crazy Search

poj1200:http://poj.org/problem?id=1200 题意:给你一个有m种字符串,求长度为n的连续子串由多少种. 题解:网上的代码都是hash,但是本人觉得hash有问题,就是n,m稍微大点,hash的值都会爆出int,无法开数组来记录该串是否被记录.可能数据弱,结果hash竟然A了 .正确的解法是用set来存,把所有的hash值放进set,set有去重效果,最后求一下set的大小.但是这样结果是T了.不知道这题怎么解.一下是第一种代码.于是换别的,trie树来搞. Cr

poj 1200 Crazy Search

题目: 链接:点击打开链接 题意: 输入n和nc,以及字符串s,输出长度为n的不同字串的个数. 算法: 思路: 用hash判重(hash值......),看了大牛的代码,对hash还是不甚理解.... 代码: #include<iostream> #include<cstring> #include<cstdio> #include<cstdio> using namespace std; #define MAXN 16000010 const int MA

Oracle开发之窗口函数 rows between unbounded preceding and current row

目录=========================================1.窗口函数简介2.窗口函数示例-全统计3.窗口函数进阶-滚动统计(累积/均值)4.窗口函数进阶-根据时间范围统计5.窗口函数进阶-first_value/last_value6.窗口函数进阶-比较相邻记录 一.窗口函数简介: 到目前为止,我们所学习的分析函数在计算/统计一段时间内的数据时特别有用,但是假如计算/统计需要随着遍历记录集的每一条记录而进行呢?举些例子来说: ①列出每月的订单总额以及全年的订单总额②

HDU 5325 Crazy Bobo

对原来的边(u, v)  方向定为u->v当w[u] > w[v] 最大Set是max{u到达的点集合} Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 382    Accepted Submission(s): 116 Problem Description Bobo has a tree,whose ve

ZOJ 3524 Crazy Shopping

Crazy Shopping Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 352464-bit integer IO format: %lld      Java class name: Main Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Kawashiro