MMATRIX - SHIFT Operator on Matrix

Tác giả: flashmt

Ngôn ngữ: Pascal

uses math;
const fi='';
      oo=1000000000;
var n,re,m:longint;
    a:array[0..6,0..6] of longint;
    s:array[0..6] of longint;

procedure rf;
var i,j:longint;
begin
     re:=-oo;
     fillchar(s,sizeof(s),0);
     for i:=0 to n-1 do
          for j:=0 to n-1 do
          begin
               read(a[i,j]);
               s[j]:=s[j]+a[i,j];
          end;
     for j:=0 to n-1 do re:=max(re,s[j]);
end;

procedure att(i,val:longint);
var j,k,m:longint;
begin
     m:=-oo;
     if i>=n then
     begin
          re:=val; exit;
     end;
     for j:=0 to n-1 do
     begin
          s[j]:=s[j]+a[i,j];
          m:=max(m,s[j]);
     end;
     if m<re then att(i+1,m);
     for k:=1 to n-1 do
     begin
          m:=-oo;
          for j:=0 to n-1 do
          begin
               s[j]:=s[j]+a[i,(j+k) mod n]-a[i,(j+k-1) mod n];
               m:=max(m,s[j]);
          end;
          if m<re then att(i+1,m);
     end;
     for j:=0 to n-1 do s[j]:=s[j]-a[i,(j+n-1) mod n];
end;

begin
     assign(input,fi); reset(input);
     while true do
     begin
          read(n);
          if n=-1 then break;
          rf;
          fillchar(s,sizeof(s),0);
          att(0,m);
          writeln(re);
     end;
     close(input);
end.

Download