Cod sursa(job #201136)

Utilizator cypherMircea Grecu cypher Data 29 iulie 2008 10:27:53
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.38 kb
program flip_002;
var a,a1:array[1..20,1..20] of longint;
	sn,sm:array[1..20] of longint;
	b,c:array[0..40] of byte;
	n,m,n1,m1:byte;
	sum:longint;
	
	procedure citire;
	var f:text; i,j:byte;
	begin
		assign(f,'flip.in'); reset(f);
		readln(f,n,m); sum:=0;
		for i:=1 to n do begin
			for j:=1 to m do begin
				read(f,a[i,j]);
				inc(sn[i],a[i,j]);
				inc(sm[j],a[i,j]);
				inc(sum,a[i,j]);
			end;
		end;
		a1:=a;
		n1:=n div 2; m1:=m div 2;
		if sum<0 then sum:=-sum;
		close(f);
	end;
	
	procedure solutie(i,j:byte);
	var x,y:byte; t:longint;
	begin
		a:=a1;
		for x:=1 to i do begin
			for y:=1 to m do a[b[x],y]:=-a[b[x],y];
		end;
		for y:=1 to j do begin
			for x:=1 to n do a[x,c[j]]:=-a[x,c[j]];
		end;
		t:=0;
		for x:=1 to n do begin
			for y:=1 to m do inc(t,a[x,y]);
		end;
		if t>sum then sum:=t;
		if (-t)>sum then sum:=(-t);
	end;
	
	procedure go(i,j,k:byte);
	var l:byte;
	begin
		if k=1 then begin
			if i<=n1 then
			for l:=b[i-1]+1 to n do begin
				b[i]:=l; solutie(i,j);
				go(i,1,2); go(i+1,0,1);
			end;
		end
		else begin
			if j<=m1 then
			for l:=c[j-1]+1 to m do begin
				c[j]:=l; solutie(i,j);
				go(i,j+1,2);
			end;
		end;
	end;
	
	procedure scriere;
	var f:text;
	begin
		assign(f,'flip.out'); rewrite(f);
		writeln(f,sum);
		close(f);
	end;
	
BEGIN
	citire;
	go(1,0,1);
	go(0,1,2);
	scriere;
END.