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 }
View Code

 

转载于:https://www.cnblogs.com/wangmengmeng/p/5323218.html

### 关于 `rows` 函数的理解 在编程领域中,`rows` 并不是一个标准的内置函数名,但在某些特定场景下可能作为自定义函数或者库的一部分存在。以下是几种常见的可能性: #### 1. 数据处理中的 `rows` 方法 在数据科学和数据分析工具(如 Pandas 库)中,虽然没有直接名为 `rows` 的函数,但可以通过类似的方法访问 DataFrame 中的行数据。 ```python import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # 访问每一行的数据 for index, row in df.iterrows(): print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}")[^3] ``` 上述代码展示了如何通过 `iterrows()` 遍历 DataFrame 的每一行,类似于操作所谓的 “rows”。 --- #### 2. SQL 查询中的 ROWS 子句 在 SQL 编程中,`ROWS` 是一个关键字,通常用于窗口函数的范围限定。它允许开发者指定基于当前行的偏移量来计算聚合值。 示例: ```sql SELECT department, employee_name, salary, AVG(salary) OVER (PARTITION BY department ORDER BY salary DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS avg_salary FROM employees; ``` 此查询按部门分区并计算到当前行为止的平均工资[^2]。 --- #### 3. 自定义 `rows` 函数 如果开发人员希望创建自己的 `rows` 函数,则可以根据需求设计其功能。例如,在 Python 中可以编写如下函数以提取矩阵中的所有行: ```python def rows(matrix): """Return all the rows of a matrix.""" return matrix[:] # 返回整个二维数组的所有行 matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] result = rows(matrix) print(result) # 输出:[[1, 2, 3], [4, 5, 6], [7, 8, 9]][^4] ``` 该例子展示了一个简单的自定义函数实现方式。 --- #### 总结 由于上下文中未明确指出具体环境下的 `rows` 函数含义,因此提供了多种解释方向。如果是针对某种特殊框架或语言,请提供更多背景资料以便进一步确认适用情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值