AOJ 0525 Osenbei

编程语言: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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值