Cod sursa(job #670875)

Utilizator mitrutstrutMitrea Andrei Ionut mitrutstrut Data 30 ianuarie 2012 12:33:28
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream ifile;
ofstream ofile;
int a,b,i,j,best=0,m[20][16],bk[20];
 void resolve(){
int x,s=0;
for(j=1;j<=b;j++){
x=0;
for(i=1;i<=a;i++){
x = x + bk[i]*m[i][j];
}
if(x<0) s -= x;
else s += x;
}
if(s>best) best = s;
}
void bktr(int k){
if(k==a+1) resolve();
else {
bk[k] = -1;
bktr(k+1);
bk[k] = 1;
bktr(k+1);
}
}
int main(){
ifile.open("flip.in");
ifile>>a>>b;
for(i=1; i<=a; i++){
for(j=1; j<=b; j++){
ifile>>m[i][j];
best += m[i][j];
}
}
ifile.close();
bktr(1);
ofile.open("flip.out");
ofile<<best;
ofile.close();
return 0;   
}