编程语言:Java
题目链接:https://onlinejudge.u-aizu.ac.jp/problems/0525
题解:用 f 数组来表示每层的翻转情况,为false代表该层未翻转,为true代表该层翻转,由于每一种情况都会遍历,所以不需要将 f 数组初始化为全false,但是改变其状态的时候必须要取反;剩下的就是列出所有行翻转与不翻转的种种情况,统计最大值即可
结果:AC
import java.io.*;
import java.util.Scanner;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(System.in);
static int[][] a=new int[10][10000];
static boolean[] f=new boolean[10];
static int r,c;
static int res;
public static void main(String[] args) throws IOException {
for(;;){
r=sc.nextInt();
c=sc.nextInt();
if(r==0&&c==0)break;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
a[i][j]=sc.nextInt();
}
}
res=0;
//Arrays.fill(f,false);
dfs(0);
out.println(res);
}
out.flush();
}
private static void dfs(int step) {
if(step==r){
int ans=0;
for(int i=0;i<c;i++){
int d=0;
for(int j=0;j<r;j++){
if(a[j][i]==1&&!f[j])
d++;
if(a[j][i]==0&&f[j])
d++;
}
ans+=Math.max(d,r-d);
}
res=Math.max(res,ans);
return;
}
dfs(step+1);
//f[step]=true;
f[step]=!f[step];
dfs(step+1);
}
}