Cod sursa(job #552795)

Utilizator moonRadu Chichi moon Data 12 martie 2011 20:53:01
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include<fstream>
#include<iostream>
using namespace std;

long a[25][25],v[25],v2[25],n,m,tott=0,maxx=0;
int rec(int x,int y)
{
	int i,j,s;
	if(x+y!=m+n)
	{
	if(y==m)
	rec(x+1,1);
	else rec(x,y+1);

		if(!v[x])
		{
			for(j=1;j<=m;j++)
			a[x][j]*=-1;
			v[x]=1;
			if(y==m)
			rec(x+1,1);
			else rec(x,y+1);
			for(j=1;j<=m;j++)
			a[x][j]*=-1;
			v[x]=0;
		}
		

	
		else if(!v2[y])
		{
			
		for(j=1;j<=n;j++)
			a[j][y]*=-1;	
			v2[y]=1;
			if(y==m)
			rec(x+1,1);
			else rec(x,y+1);
			for(j=1;j<=n;j++)
			a[j][y]*=-1;	
			v2[y]=0;
		}
		
	}
	
	else
	{
	s=0;
		for(i=1;i<=x;i++)
		{
			for(j=1;j<=y;j++)
			{
				s+=a[i][j];
			//	g<<a[i][j]<<" ";
			}
			tott++;
			//g<<'\n';
		}
		//g<<'\n';
		if(s>maxx) maxx=s;
		return 0;
	}
}
int main()
{
	int i,j;
	ifstream f("flip.in");
	ofstream g("flip.out");
	f>>n>>m;
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			f>>a[i][j];
		
	rec(1,1);
	g<<maxx;
	
}