Crazy Rows (GoogleCode Jam 2009 Round2 A)
来源:互联网 发布:手机声音扩大软件 编辑:程序博客网 时间:2024/05/29 04:58
来自《挑战程序设计竞赛》
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, T. T 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: 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
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 50int 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;}
- Crazy Rows (GoogleCode Jam 2009 Round2 A)
- crazy Rows 2009 Round2 A
- Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心
- 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
- [Code Jam] Crazy Rows
- Google Code Jam 2009 Round2 A (脑洞题)
- Google Code Jam Round2 A,B题解
- GCJ 2009 Round 2 Problem A. Crazy Rows
- GCJ Round 2 2009 Problem A. Crazy Rows
- 挑战2.7.2 Round 2 2009 A. Crazy Rows 贪心
- Crazy Rows
- GCJ 2009 Problem A. Crazy Rows【位运算要加LL】
- Google Code Jam 2016 Round2
- Google Code Jam Round2 题解报告 Problem A.Cheating a Boolean Tree
- Select removing dropdown arrow
- C盘根目录下只能创建文件夹不能新建文件的解决办法
- LeetCode 354. Russian Doll Envelopes
- 人为构建电子电路的本质
- Python新路程(3)Python中的注释等小TIPS
- Crazy Rows (GoogleCode Jam 2009 Round2 A)
- 从根源上解析 Java volatile 关键字的实现
- RecyclerView自定义LayoutManager,打造不规则布局
- linux安装mjpg-stream
- Map遍历
- Design Pattern: 简单工厂模式 v.s. 工厂方法模式
- HDU-1082
- Java基础--集合Set-HashSet
- 蚍蜉叹