Cod sursa(job #18577)

Utilizator vanila0406Ionescu Victor vanila0406 Data 18 februarie 2007 12:40:51
Problema Zone Scor 30
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasa a 9-a si gimnaziu Marime 4.2 kb
program zone;
var f,g:text;
        a:array[0..513,0..513] of int64;
        n:longint;
        v,s:array[1..9] of int64;


procedure iofile;
var i,j,aux,x:longint;
begin
        fillchar(a,sizeof(a),0);
        assign(f,'zone.in');
        reset(f);
        assign(g,'zone.out');
        rewrite(g);
        readln(f,n);
        for i:=1 to 9 do
                read(f,v[i]);
        for i:=1 to 8 do
                for j:=i+1 to 9 do
                        if v[i]>v[j] then
                                begin
                                        aux:=v[i];
                                        v[i]:=v[j];
                                        v[j]:=aux;
                                end;
        readln(f);
        for i:=1 to n do
                for j:=1 to n do
                        begin
                                read(f,x);
                                a[i,j]:=a[i-1,j]+a[i,j-1]+x-a[i-1,j-1];
                        end;
        close(f);
end;


function verif:boolean;
var i:longint;
begin
        verif:=true;
        for i:=1 to 9 do
                if s[i]<>v[i] then
                        begin
                                verif:=false;
                                exit;
                        end;
end;



procedure pozitie(var m:longint;p,u:longint);
var i,j,di,dj,aux:longint;
begin
        i:=p;
        j:=u;
        di:=0;
        dj:=-1;
        while i<j do
                begin
                        if s[i]>s[j] then
                                begin
                                        aux:=di;
                                        di:=-dj;
                                        dj:=-aux;
                                        aux:=s[i];
                                        s[i]:=s[j];
                                        s[j]:=aux;
                                end;
                        i:=i+di;
                        j:=j+dj;
                end;
        m:=i;
end;


procedure quick(p,u:longint);
var m:longint;
begin
        if p<u then
                begin
                        pozitie(m,p,u);
                        quick(p,m-1);
                        quick(m+1,u);
                end;
end;



procedure prel;
var l1,l2,c1,c2,i,j:longint;
begin
        for l1:=1 to n-2 do
                for l2:=l1+1 to n-1 do
                        for c1:=1 to n-2 do
                                for c2:=c1+1 to n-1 do
                                begin
                                        s[1]:=a[l1,c1];
                                        s[4]:=a[l2,c1]-a[l1,c1];
                                        s[7]:=a[n,c1]-a[l2,c1];
                                        s[2]:=a[l1,c2]-a[l1,c1];
                                        s[5]:=a[l2,c2]-a[l2,c1]-s[2];
                                        s[8]:=a[n,c2]-a[n,c1]-s[2]-s[5];
                                        s[3]:=a[l1,n]-a[l1,c2];
                                        s[6]:=a[l2,n]-a[l2,c2]-s[3];
                                        s[9]:=a[n,n]-a[n,c2]-s[6]-s[3];
                                        {for i:=1 to 8 do
                                                for j:=i+1 to 9 do
                                                        if s[i]>s[j] then
                                                                begin
                                                                        aux:=s[i];
                                                                        s[i]:=s[j];
                                                                        s[j]:=aux;
                                                                end; }
                                        quick(1,9);
                                        if verif then
                                                begin
                                                        writeln(g,l1,' ',l2,' ',c1,' ',c2);
                                                        close(g);
                                                        halt;
                                                end;
                                end;
end;



begin
        iofile;
        prel;
end.