Cod sursa(job #201146)

Utilizator cypherMircea Grecu cypher Data 29 iulie 2008 11:19:48
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.52 kb
program flip_002;
var a,a1:array[1..20,1..20] of longint;
	sn,sm:array[1..20] of longint;
	b,c,d,e:array[0..20] 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(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,k:byte);
	var x,y:byte; t:longint;
	begin
		a:=a1;
		t:=0;
		if k=1 then begin
			fillchar(sm,sizeof(sm),0);
			for x:=1 to i do
				for y:=1 to m do a[b[x],y]:=-a[b[x],y];
			for x:=1 to n do
				for y:=1 to m do inc(sm[y],a[x,y]);
			for y:=1 to m do t:=t+abs(sm[y]);
		end
		else begin
			fillchar(sn,sizeof(sn),0);
			for x:=1 to n do begin
				for y:=1 to i do a[x,b[y]]:=-a[x,b[y]];
			end;
			for x:=1 to n do
				for y:=1 to m do inc(sn[x],a[x,y]);
			for x:=1 to n do t:=t+abs(sn[x]);
		end;
		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,k);
				go(i+1,0,1);
			end;
		end
		else begin
			if j<=m1 then
			for l:=b[j-1]+1 to m do begin
				b[j]:=l; solutie(j,k);
				go(0,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.