Crazy Rows (GoogleCode Jam 2009 Round2 A)

本文介绍了一种针对特定矩阵(N x N,元素为0和1)通过最少次数的行交换操作,使得所有1位于或低于主对角线的算法。该算法适用于竞赛编程,并提供了解题思路及AC代码实现。

来自《挑战程序设计竞赛》

1.题目原文

https://code.google.com/codejam/contest/204113/dashboard#s=p0

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

2.解题思路

最先想到的是尝试所有N!种方案,但是在Large中最大的N=40,肯定会超时。
暂时先考虑哪一行应该放在第一行。这一行应具有10000……或者00000……的形式。可以交换到第一行的当然也可以交换到第二行。当有多个条件满足时,选择离第一行最近的行对应的最终所需步数最小。确定第一行之后,剩余的行类似可以解决。
每行的0和1的位置不重要,只要知道每行最后一个1的位置即可。
如果预先处理好这些位置,就可以降低时间复杂度。时间复杂度为O(N^2)。

3.AC代码

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;

#define maxn 50

int N;
int M[maxn][maxn];//矩阵

int a[maxn];//a[i]表示第i行最后出现1的位置

void solve()
{
    int res=0;
    for(int i=0;i<N;i++){
        a[i]=-1;//如果不含1,就当作-1
        for(int j=0;j<N;j++){
            if(M[i][j]==1){
                a[i]=j;
            }
        }
    }
    for(int i=0;i<N;i++){
        int pos=-1;//要移动到第i行的行
        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-1]);
            res++;
        }
    }
    printf("%d\n",res);
}
int main()
{
    freopen("A-large-practice.in","r",stdin);
    freopen("A-large-practice.out","w",stdout);
    int t,kase=0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&N);
        for(int i=0;i<N;i++){
            char s[maxn];
            cin>>s;
            for(int j=0;j<N;j++){
                M[i][j]=s[j]-'0';
            }
        }
        printf("Case #%d: ",++kase);
        solve();
    }
    return 0;
}


`<textarea>` 的 `rows` 属性用于**设置文本区域的可见行数**,它决定了 textarea 在页面上默认显示多少行的高度。这个属性是 HTML 原生支持的,值是一个正整数(如 1、2、3……),表示以当前字体大小和行高为基准,显示多少行文本。 --- ### ✅ `rows="1"`、`rows="2"`、`rows="3"` 的区别 | `rows` 值 | 含义 | 可视高度 | 示例场景 | |----------|------|---------|--------| | `rows="1"` | 显示 **1 行** 文本的高度 | 最矮,适合短输入 | 搜索框、标题输入 | | `rows="2"` | 显示 **2 行** 文本的高度 | 中等高度 | 简短备注、留言 | | `rows="3"` | 显示 **3 行** 文本的高度 | 较高,能看更多内容 | 备注、描述信息 | > 📌 注意:这只是“初始可见行数”,不影响实际可输入的内容长度(由 `maxlength` 控制)或是否自动扩展(由 `autosize` 控制)。 --- ### 🔍 具体表现差异(代码演示) ```html <!-- 1 行 textarea --> <textarea rows="1" cols="30" placeholder="这是 1 行"></textarea> <!-- 2 行 textarea --> <textarea rows="2" cols="30" placeholder="这是 2 行(更高)"></textarea> <!-- 3 行 textarea --> <textarea rows="3" cols="30" placeholder="这是 3 行(最高)"></textarea> ``` #### 渲染效果: - `rows="1"`:高度 ≈ 1 行文字的高度(约 20px~24px,取决于字体) - `rows="2"`:高度 ≈ 2 倍行高 - `rows="3"`:高度 ≈ 3 倍行高 你可以通过浏览器开发者工具查看它们的计算高度不同。 --- ### ⚠️ 注意事项 1. **`rows` 不限制输入行数** - 用户仍然可以输入超过 `rows` 数量的文字,滚动条会出现。 - 如果你希望自动扩展高度,请结合使用 `autosize`(在 Vue 中使用 Vant 等框架时常见)。 2. **与 CSS 高度冲突时,CSS 优先** ```css textarea { height: 100px; } ``` 此时 `rows` 可能失效,因为显式设置了高度。 3. **移动端适配建议** - `rows="2"` 或 `rows="3"` 更适合移动设备上的表单输入,提供更好的用户体验。 - 单行输入建议用 `<input type="text">` 而不是 `<textarea rows="1">`,语义更清晰。 --- ### 💡 实际应用示例(Vue + Vant) ```vue <van-field v-model="message" rows="1" type="textarea" placeholder="简短评价" /> <van-field v-model="message" rows="3" type="textarea" placeholder="详细描述..." /> ``` - `rows="1"`:看起来像一个加长版 input,但仍是 textarea。 - `rows="3"`:用户一打开就能看到更多输入空间,体验更好。 --- ### ✅ 总结 | 特性 | `rows="1"` | `rows="2"` | `rows="3"` | |--------------|------------------|------------------|------------------| | 初始高度 | 最小 | 中等 | 较大 | | 输入容量感知 | 弱(像单行) | 适中 | 强(明显多行) | | 使用场景 | 短文本(标签、备注)| 中等长度输入 | 描述、评论等长文本 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值