Pagini recente » Cod sursa (job #450096) | Cod sursa (job #1016193) | Cod sursa (job #732730) | Cod sursa (job #2437661) | Cod sursa (job #1352827)
#include<stdio.h>
int N,M;
int bkt(int sum_row[N], int sum_col[M], int sum_matrix, int max){
int i,j, max_bkt;
for(i=0; i<N; i++)
// if(sum_row[i] < 0)
if(sum_matrix - 2*sum_row[i] > max){
sum_row[i] *= -1;
int sum = sum_matrix + 2 * sum_row[i];
max_bkt = bkt(sum_row, sum_col, sum, sum);
sum_row[i] *= -1;
if(max_bkt > max)
max = max_bkt;
}
for(j=0; j<M; j++)
// if(sum_col[j] < 0)
if(sum_matrix - 2*sum_col[j] > max){
sum_col[j] *= -1;
int sum = sum_matrix + 2 * sum_col[j];
max_bkt = bkt(sum_row, sum_col, sum, sum);
sum_col[j] *= -1;
if(max_bkt > max)
max = max_bkt;
}
return max;
}
int main(char *argv[]){
FILE *f = fopen("flip.in", "rt");
int i,j;
fscanf(f, "%d %d", &N, &M);
int a[N][M], sum_row[N], sum_col[M], sum_matrix = 0, max;
for(i=0; i<N; i++)
sum_row[i] = 0;
for(i=0; i<M; i++)
sum_col[i] = 0;
for(i=0; i<N; i++)
for(j=0; j<M; j++){
fscanf(f,"%d", &a[i][j]);
sum_row[i] += a[i][j];
sum_col[j] += a[i][j];
sum_matrix += a[i][j];
}
max = sum_matrix;
max = bkt(sum_row, sum_col, sum_matrix, max);
FILE *fout = fopen("flip.out", "wt");
fprintf(fout, "%d", max);
fclose(f);
fclose(fout);
return 0;
}