Cod sursa(job #6972)

Utilizator mithyPopovici Adrian mithy Data 21 ianuarie 2007 11:17:09
Problema Elimin Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasa a 10-a Marime 1.19 kb
#include <fstream.h>

int a[100][100],n,m,r,c;
int row[100];
int col[100];
int maxim;

void read();
void make();
void perm(int [], int);
void init(int [], int, int);

int main()
{
	read();
	make();
	return 0;
}

void read()
{
	int i,j;
	ifstream fin("elimin.in");
	fin >> n >> m;
	fin >> r >> c;

	for (i=1; i<=n; i++)
	{
		for (j=1; j<=m; j++)
		{
			fin >> a[i][j];
			a[0][j] += a[i][j];
			a[i][0] += a[i][j];
			maxim += a[i][j];
		}
	}
	init(row,r,n);
}

void perm(int a[],int n)
{
	int i,aux;
   aux = a[n-1];
   for (i=n-1;i>0;i--)
   {
      a[i] = a[i-1];
   }
   a[0] = aux;
}

void init(int a[], int n, int m)
{
	int i;
	for (i=0; i<n; i++) a[i] = 1;
   for (i; i<m; i++) a[i] = 0;
}

void make()
{
	int aux,i,j,max=0,ok=1;
	while (ok)
	{
		init (col,c,m);
		while (col[m-1]!=1)
		{
			aux = 0;
			for (i=1; i<=n; i++)
			{
				for (j=1; j<=m; j++)
				{
					if ( row[i-1] && col[j-1] )
					{
						aux = maxim - ((a[0][j] + a[i][0]) - a[i][j]);
						if (aux > max) max = aux;
					}
				}
			}
			perm(col,m);
		}
      if (row[n-1]==1) ok = 0;
		perm(row,n);
	}

	ofstream fout("elimin.out");
	fout << max << '\n';
}