Cod sursa(job #532060)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 10 februarie 2011 19:51:21
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream.h>
using namespace std;
int N , M , nr , i , j , V[17][17] , sum_tot , S[17] , l , E[17][17] , sum , maxim;
int main()
{
	ifstream f("flip.in");
	ofstream g("flip.out");
	f>>N>>M;nr=1;
	for(i=1;i<=N;i++){
		for(j=1;j<=M;j++){
			f>>V[i][j];
		}
	}
	for(i=1;i<=M;i++){
		nr*=2;
	}
	for(i=1;i<=nr;i++){
		sum_tot=0;
		for(j=1;j<=M;j++){
			if(S[j]==1){
				for(l=1;l<=N;l++){
					E[l][j]=-V[l][j];
				}
			}
			else{
				for(l=1;l<=N;l++){
					E[l][j]=V[l][j];
				}
			}
		}
		for(j=1;j<=N;j++){
			sum=0;
			for(l=1;l<=M;l++){
				sum+=E[j][l];
			}
			if(sum<0){
				sum_tot+=-sum;
			}
			else{
				sum_tot+=sum;
			}
		}
		if(sum_tot>maxim){
			maxim=sum_tot;
		}
		for(j=M;j>=1;j--){
			if(S[j]==1){
				S[j]=0;
			}
			else{
				S[j]=1;
				break;
			}
		}
	}
	g<<maxim;
	f.close();
	g.close();
	return 0;
}