Aizu 0525(Osenbei 穷竭搜索)

来源:互联网 发布:nba季后赛各项数据统计 编辑:程序博客网 时间:2024/06/03 09:50
/* 翻煎饼,1正面朝上,0反面朝上, * 每次可翻某一行或者某一列 * 问最大正面朝上数 */#include<cstdio>#include<iostream>#include<vector>#include<map>#include<set>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cstdlib>#include<cctype>#include<sstream>#include<stack>#include<functional>using namespace std;typedef long long ll;typedef pair<int, int> P;const int INF=0x3f3f3f3f;const int MAX_N = 10000;int G[10][MAX_N];int R, C;int ans;//反转r行void turn(int r){    for(int i = 0; i < C; i++)        G[r][i] = !G[r][i];}//计算当前操作后,煎饼朝上的数目int num(){    int res = 0;    for(int i = 0; i < C; i++){        int t = 0;        for(int j = 0; j < R; j++){            if(G[j][i])t++;        }        res += max(t, R - t);    }    return res;}//行数较少,只用考虑行的翻转,每列只考虑翻转或者不翻转//这在num()体现出来void dfs(int r){    if(r >= R){        ans = max(ans, num());        return;    }    turn(r);    dfs(r + 1); //翻当前行    turn(r);    dfs(r + 1); //不翻当前行}int main(){#ifdef LOCALfreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endif//ios::sync_with_stdio(false);while(cin >> R >> C && (R || C)){        ans = -INF;        for(int i = 0; i < R; i++)            for(int j = 0; j < C; j++)                scanf("%d", &G[i][j]);        dfs(0);        cout << ans << endl;}return 0;}

0 0
原创粉丝点击