Cod sursa(job #19165)

Utilizator maria_pparcalabescu maria daniela maria_p Data 18 februarie 2007 20:34:34
Problema Zone Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.9 kb
var f,g:text;
    sume,b:array[0..9]of longint;
    a,s:array[0..600,0..600]of longint;
    i,l1,l2,c1,p,c2,n,j,k:longint;
    ok,ok1:boolean;
begin
assign(f,'zone.in');reset(f);
assign(g,'zone.out');rewrite(g);
readln(f,n);
for i:=1 to  9 do
        read(f,sume[i]);
l1:=0;c1:=0;ok:=true;
for i:=1 to n do
    for j:=1 to n do
        begin
        read(f,a[i,j]);
        s[i,j]:=s[i-1,j]+s[i,j-1]-s[i-1,j-1]+a[i,j];
        if ok then
           for k:=1 to 9 do
               if s[i,j]=sume[k] then begin
                                      l1:=i;
                                      c1:=j;
                                      p:=k;
                                      ok:=false;
                                      end;
        end;
while (not ok)and(l1<n) do
      begin
      for i:=1 to 9 do b[i]:=sume[i];
      b[p]:=0;p:=0;
      l2:=0;c2:=0;
      for i:=c1+1 to n-1 do
          if c2=0 then begin
                       for j:=1 to 9 do
                           if s[l1,i]-s[l1,c1]=b[j] then begin
                                                   c2:=i;
                                                   b[j]:=0;
                                                   break;
                                                   end;
                       end
                  else break;
      for i:=l1+1 to n-1 do
          if l2=0 then begin
                       for j:=1 to 9 do
                           if s[i,c1]-s[l1,c1]=b[j] then begin
                                                   l2:=i;
                                                   b[j]:=0;
                                                   break;
                                                   end;
                       end
                  else break;
      for i:=1 to 9 do
          begin
          if s[l1,n]-s[l1,c2]=b[i] then b[i]:=0;
          if s[l2,c2]-s[l1,c2]-s[l2,c1]+s[l1,c1]=b[i] then b[i]:=0;
          if s[l2,n]-s[l1,n]-s[c2,l2]+s[l1,c2]=b[i] then b[i]:=0;
          if s[n,c2]-s[l2,c2]-s[n,c1]+s[l2,c1]=b[i] then b[i]:=0;
          if s[n,n]-s[l2,n]-s[n,c2]+s[l2,c2]=b[i] then b[i]:=0;
          if s[n,c1]-s[l2,c1]=b[i] then b[i]:=0;
          end;
      ok1:=true;
      for i:=1 to 9 do
          if b[i]<>0 then ok1:=false;
      if not ok1 then
         for i:=l1 to n-1 do
             for j:=1 to n-1 do
                 if not ok then
                 for k:=1 to 9 do
                     if (s[i,j]=sume[k])and(i<>l1)and(j<>c1) then begin
                                            l1:=i;
                                            c1:=j;
                                            p:=k;
                                            ok:=true;
                                            end;
      if ok and (p<>0) and not ok1 then ok:=false;
      if ok1 then ok:=true;
      end;
writeln(g,l1,' ',c1,' ',l2,' ',c2);
close(f);
close(g);
end.