GCJ--Crazy Rows (2009 Round2 A)
来源:互联网 发布:华测gps怎么导出数据 编辑:程序博客网 时间:2024/06/08 14:21
Problem
You are given an N x N matrix with 0 and 1 values. You can swap any twoadjacent 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, T. T test cases follow.
The first line of each test case has one integer, N. Each of the nextN lines contains N characters. Each character is either 0 or 1.
Output
For each test case, output
Case #X: Kwhere 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
以下是我的AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define MAX_N 45using namespace std;char maze[MAX_N][MAX_N];int n,temp;int T;int a[MAX_N];void solve(){int res=0;for(int i=0;i<n;i++){a[i]=-1;for(int j=0;j<n;j++){if(maze[i][j]=='1')a[i]=j;}}for(int i=0;i<n;i++){int pos=-1;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("Case #%d: %d\n",temp-T,res);}int main(){//freopen("A-large-practice.in","r",stdin);//freopen("output.out","w",stdout);scanf("%d",&T);temp=T;while(T--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",maze[i]);}solve();}return 0;}
- GCJ 2009 Round2 A (Crazy Rows)
- GCJ--Crazy Rows (2009 Round2 A)
- GCJ 2009 Round2 Problem A. Crazy Rows
- GCJ 2009 Round2 A Crazy Rows
- 每天水一水 Crazy Rows (2009 Round2 A) GCJ
- crazy Rows 2009 Round2 A
- Crazy Rows (GoogleCode Jam 2009 Round2 A)
- GCJ 2009 Round 2 Problem A. Crazy Rows
- GCJ Round 2 2009 Problem A. Crazy Rows
- GCJ Crazy Rows
- Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心
- GCJ 2009 Problem A. Crazy Rows【位运算要加LL】
- GCJ 2009 Round2 A 构造贪心策略
- GCJ Crazy Rows 水题-贪心
- Crazy Rows————GCJ 2009 Round2A(简单贪心)
- 挑战2.7.2 Round 2 2009 A. Crazy Rows 贪心
- Crazy Rows
- [Code Jam] Crazy Rows
- 关于RunLoop和Timer的一点理解
- 清除行列
- Unity IOS端 注意点
- 广师Android群分享之自定义控件
- Struts2学习笔记之自定义转换器
- GCJ--Crazy Rows (2009 Round2 A)
- 【水题算法】不用+运算符求加法
- Chrome调试css(chrome开发工具workspace使用)
- 安卓 java.lang.StackOverflowError 错误检查,特别是部分低端机型
- ios中常用英语单词汇总
- linux 查找 方法
- Android 使用小技巧之 :new Handler().postDelayed() 延迟intent跳转
- 写一个虚拟机的模型
- ibatis录入insert之后,返回主键