Crazy Rows (贪心)

标签: tan xin / bai shu
8人阅读 评论(0) 收藏 举报
分类:

Crazy Rows

You are given an N x N matrix with 0 and 1 values. You can swap any two adjacentrows 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 N characters. 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

1 ≤ N ≤ 8

Input
 
Output 
 
3
2
10
11
3
001
100
010
4
1110
1100
1100
1000
Case #1: 0
Case #2: 2

Case #3: 4


贪心算法:一行一行走,首先从第一行从上往下找到第一个符合一行的交换下,然后再依次以这种方法处理下面几行

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

const int maxn=50;

char m[maxn][maxn];
int a[maxn];

int main()
{
	int t,casen=0;
	scanf("%d",&t);
	while(t--){
		memset(a,0,sizeof(a));//这里的处理很关键,因为多组输入,a会残留上次的结果 
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%s",m[i]+1);
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(m[i][j]=='1'){
					a[i]=j;
				}
			}
		}
	/*	for(int i=1;i<=n;i++){
			cout<<a[i]<<" ";
		}
		cout<<endl;*/
		int res=0;
		for(int i=1;i<=n;i++){
			int pos=-1;
			for(int j=i;j<=n;j++){
				if(a[j]<=i){//本题最核心的东西,找到即跳出 
					pos=j;
					break;
				}
			}
			for(int k=pos;k>i;k--){
				res++;
				swap(a[k],a[k-1]);
			}
		}
		printf("Case #%d: %d\n",++casen,res);
	}
	return 0;
}


查看评论

Crazy Rows————GCJ 2009 Round2A(简单贪心)

题目:给定一个由0和1组成的矩阵,只允许交换相邻的两行。要把矩阵化为下三角矩阵,即主对角线上方的元素都为0,问至少需要交换几次。(1 input:3            001        ...
  • say_c_box
  • say_c_box
  • 2016-07-17 09:22:40
  • 362

GCJ 2009 Round 2 Problem A. Crazy Rows

给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数。...
  • murmured
  • murmured
  • 2014-03-07 18:38:51
  • 1256

Crazy Rows

//矩阵中每行最后1的位置最为关键。另外越再前行,1的个数应越少。 //输入 int Matrix[max][max]; int _N; int a[max];//统计每行最后1的位置 int sol...
  • qq_33823833
  • qq_33823833
  • 2017-06-29 11:12:24
  • 130

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

#include using namespace std; #define maxn 10000 + 10 int M[maxn][maxn]; int mark[maxn]; int n, m...
  • DoJintian
  • DoJintian
  • 2015-04-01 17:32:18
  • 689

挑战2.7.2 Round 2 2009 A. Crazy Rows 贪心

题目链接:https://code.google.com/codejam/contest/204113/dashboard题意:题解:我们只关心每一行最后一个1的位置。 考虑第i行应该放什么,就是a...
  • yxg_123
  • yxg_123
  • 2017-03-09 11:53:55
  • 92

[Code Jam] Crazy Rows

Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of th...
  • type_q
  • type_q
  • 2014-04-27 10:02:00
  • 415

Crazy

 Start to listen to "Crazy" a lot. Listen to it in my lap-top, My mp3 player. CrazyTell me whats wro...
  • alexjames_83me
  • alexjames_83me
  • 2006-06-28 13:00:00
  • 474

GCJ Crazy Rows 水题-贪心

#include #include #include #include #include #include #include #include #include #include ...
  • qq_24667639
  • qq_24667639
  • 2015-04-20 15:01:31
  • 347

GCJ 2009 Problem A. Crazy Rows【位运算要加LL】

教训:自己预处理位运算的一些东西,1之类的要加上 LL,否则会出问题 #include #include #include #include #include #include ...
  • baidu_23081367
  • baidu_23081367
  • 2016-04-07 12:40:27
  • 247

挑战GCJ的题目: crazy row

参考ACRUSH 和 挑战程序竞赛代码。  题目如下: 链接: https://code.google.com/codejam/contest/204113/dashboard ...
  • a130737
  • a130737
  • 2015-03-04 09:11:34
  • 635
    个人资料
    持之以恒
    等级:
    访问量: 6147
    积分: 2677
    排名: 1万+
    文章存档
    最新评论