Cod sursa(job #10470)

Utilizator OXyeBadragan Silviu OXye Data 28 ianuarie 2007 15:29:05
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<stdio.h>
void main()
{
	unsigned i,j,n,m,x;
	int a[16][16],neg1,poz1,poz2,neg2,s=0;
	FILE *f;
	f=fopen("flip.in","r");
	fscanf(f,"%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
			fscanf(f,"%d",&a[i][j]);
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
			if(a[i][j]<0)
			{
				poz1=0;neg1=0;poz2=0;neg2=0;
				for(x=1;x<=n;x++)
				{
					if(a[x][j]<0)
						neg1+=a[x][j];
					else
						poz1+=a[x][j];
				}
				for(x=1;x<=m;x++)
				{
					if(a[i][x]<0)
						neg2+=a[i][x];
					else
						poz2+=a[i][x];
				}
				if(-neg1-poz1>-neg2-poz2)
				{
					if(-neg1-poz1>neg1+poz1) for(x=1;x<=n;x++) a[x][j]=-a[x][j];
				}
				else
				{
					if(-neg2-poz2>neg2+poz2) for(x=1;x<=m;x++) a[i][x]=-a[i][x];
				}
			}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
			s+=a[i][j];
	}
	fclose(f);
	f= fopen("flip.out","w");
	fprintf(f,"%d\n",s);
	fclose(f);
}