Cod sursa(job #614879)

Utilizator theavengerflorea marius florin theavenger Data 7 octombrie 2011 20:31:05
Problema Jocul Flip Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.13 kb
program e2;

var
	f_in,f_out:text;
	i,j,n,m:word;
	a:array[1..20,1..20] of longint;
	slc_n,scl_n:longint;

function slc:longint;
	var
		s_neg,s_poz,s_mat:longint;
		b:array[1..20,1..20] of longint;
		
	begin
		s_mat:=0; s_neg:=0; s_poz:=0;
		for i:=1 to n do
			for j:=1 to m do
				b[i,j]:=a[i,j];
		
		//linii
		for i:=1 to n do begin
			for j:=1 to m do 
				if (b[i,j]<0) then
					s_neg:=s_neg+b[i,j]
				else
					s_poz:=s_poz+b[i,j];
			s_neg:=s_neg*(-1);
			if (s_neg>s_poz) then
				for j:=1 to m do
					b[i,j]:=b[i,j]*(-1);
			s_neg:=0; s_poz:=0; end;
	
		//coloane
		for j:=1 to m do begin
			for i:=1 to n do
				if (b[i,j]<0) then
					s_neg:=s_neg+b[i,j]
				else
					s_poz:=s_poz+b[i,j];
			s_neg:=s_neg*(-1);
			if (s_neg>s_poz) then
				for i:=1 to n do
					b[i,j]:=b[i,j]*(-1);
			s_neg:=0; s_poz:=0; end;
				
		for i:=1 to n do
			for j:=1 to m do
				s_mat:=s_mat+b[i,j];
				
		slc:=s_mat;
	end;

function scl:longint;
	var
		s_neg,s_poz,s_mat:longint;
		b:array[1..20,1..20] of longint;
		
	begin
		s_mat:=0; s_neg:=0; s_poz:=0;
		for i:=1 to n do
			for j:=1 to m do
				b[i,j]:=a[i,j];
		
		//coloane
		for j:=1 to m do begin
			for i:=1 to n do
				if (b[i,j]<0) then
					s_neg:=s_neg+b[i,j]
				else
					s_poz:=s_poz+b[i,j];
			s_neg:=s_neg*(-1);
			if (s_neg>s_poz) then
				for i:=1 to n do
					b[i,j]:=b[i,j]*(-1);
			s_neg:=0; s_poz:=0; end;
			
		//linii
		for i:=1 to n do begin
			for j:=1 to m do 
				if (b[i,j]<0) then
					s_neg:=s_neg+b[i,j]
				else
					s_poz:=s_poz+b[i,j];
			s_neg:=s_neg*(-1);
			if (s_neg>s_poz) then
				for j:=1 to m do
					b[i,j]:=b[i,j]*(-1);
			s_neg:=0; s_poz:=0; end;
			
		for i:=1 to n do
			for j:=1 to m do
				s_mat:=s_mat+b[i,j];
				
		scl:=s_mat;
	end;
		
begin
	i:=0; j:=0; n:=0; m:=0; slc_n:=0; scl_n:=0;

	assign(f_in,'flip.in');
	assign(f_out,'flip.out');
	
	reset(f_in); rewrite(f_out);
	
	readln(f_in,n,m);
	for i:=1 to n do begin
		for j:=1 to m do
			read(f_in,a[i,j]);
		readln(f_in); end;
	
	while (slc_n<>slc) or (scl_n<>scl) do begin 
		slc_n:=slc;
		scl_n:=scl; end;
	
	if (slc_n>scl_n) then
		writeln(f_out,slc_n)
	else
		writeln(f_out,scl_n);
	
	close(f_in); close(f_out);
	
end.