zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    织梦删除不需要的文件及文件安全设置
    织梦安全设置
    阿里云一个虚拟主机安装多个织梦系统
    显示织梦模板不存在的解决方法
    织梦安装
    织梦修改数据库密码
    CSS实现兼容性的渐变背景(gradient)效果
    css边框样式、边框配色、边框阴影、边框圆角、图片边框
    织梦后台编辑器添加中文字体
    织梦安装百度编辑器
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5323218.html
Copyright © 2011-2022 走看看