PROGRAM process_countings; VAR depth_count : ARRAY[1..30,1..30] OF longint; start : ARRAY[1..30,1..30] OF boolean; result_file , process_file : TEXT; total , nodes : longint; TYPE arc = RECORD bi, bj, ei, ej, nr : byte; count : longint END; VAR arcs : ARRAY[1..1000] OF arc; nr_arcs : 0..1000; PROCEDURE init_arcs; BEGIN nr_arcs := 0 END; PROCEDURE store_arc(nbi,nbj,nei,nej,nnr : byte; ncount : longint); VAR i : byte; go : boolean; BEGIN inc(nr_arcs); i := nr_arcs; go := TRUE; WHILE go AND (i > 1) DO IF arcs[i-1].count <= ncount THEN go := FALSE ELSE BEGIN arcs[i] := arcs[i-1]; dec(i) END; WITH arcs[i] DO BEGIN bi := nbi; bj := nbj; ei := nei; ej := nej; nr := nnr; count := ncount END END; PROCEDURE print_arcs(VAR out : TEXT); VAR i : byte; BEGIN FOR i := 1 TO nr_arcs DO WITH arcs[i] DO write(out,' (',bi,',',bj,')', chr(9),'-> ' ,'(',ei,',',ej,')', chr(9),nr:5,chr(9),count,chr(11)) END; PROCEDURE init_start(n,m : byte); VAR i,j : byte; BEGIN FOR i := 1 TO n DO FOR j := 1 TO m DO start[i,j] := TRUE END; PROCEDURE remove_start(n,m,i,j : byte); BEGIN start[i,j] := FALSE; start[n+1-i,j] := FALSE; start[i,m+1-j] := FALSE; start[n+1-i,m+1-j] := FALSE END; PROCEDURE process_values(n,m,i,j : byte); VAR i1,j1,c,maxj : byte; ch : char; BEGIN IF j + j = m + 1 THEN maxj := j ELSE maxj := m; start[i,j] := FALSE; FOR i1 := 1 TO n DO FOR j1 := 1 TO maxj DO IF start[i1,j1] AND (depth_count[i1,j1] <> 0) THEN BEGIN IF (i + i1 = n + 1) AND (j + j1 = m + 1) THEN IF (i = i1) OR (j = j1) THEN c := 1 ELSE c := 2 ELSE IF ((i + i1 = n + 1) OR (j + j1 = m + 1)) AND ((i = i1) OR (j = j1)) THEN c := 2 ELSE c := 4; store_arc(i,j,i1,j1,c,depth_count[i1,j1]); total := total + depth_count[i1,j1] * c; nodes := nodes + c; END; remove_start(n,m,i,j); END; PROCEDURE process_eq_values(n,i,j : byte); VAR i1,j1,c,maxj : byte; PROCEDURE add; BEGIN IF start[i1,j1] AND (depth_count[i1,j1] <> 0) THEN BEGIN IF (i + i1 = n + 1) AND (j + j1 = n + 1) THEN IF (i = i1) OR (j = j1) OR (i = j) OR (i + j = n + 1) THEN c := 2 ELSE c := 4 ELSE IF ((i + i1 = n + 1) AND (j = j1)) OR ((j + j1 = n + 1) AND (i = i1)) OR ((i + i1 = n + 1) AND (i = i1)) OR ((j + j1 = n + 1) AND (j = j1)) OR ( (abs(i - i1) = abs(j - j1)) AND ( (i + i1 = j + j1) OR(i + i1 + j + j1 = n + n + 2) ) ) THEN c := 4 ELSE c := 8; store_arc(i,j,i1,j1,c,depth_count[i1,j1]); total := total + depth_count[i1,j1] * c; nodes := nodes + c; END; END; VAR ch : char; BEGIN write(i:3,j:3); start[i,j] := FALSE; IF i = j THEN FOR i1 := 1 TO n DO FOR j1 := i1 TO n DO add ELSE IF j + j = n + 1 THEN FOR i1 := 1 TO n DO FOR j1 := j TO n DO add ELSE FOR i1 := 1 TO n DO FOR j1 := 1 TO n DO IF (j1 <> i) OR (i1 + j <> n + 1) THEN add; remove_start(n,n,i,j); remove_start(n,n,j,i); END; PROCEDURE process_one_size; VAR ch : char; n,m,i,j : byte; nm : integer; count : longint; i1,j1 : byte; first : boolean; BEGIN nodes := 0; total := 0; first := TRUE; init_arcs; REPEAT read(result_file,ch); IF ch = 't' THEN BEGIN readln(result_file); readln(result_file); readln(result_file) END ELSE BEGIN readln(result_file,n,m,i,j,count); IF first THEN BEGIN init_start(n,m); write(process_file, 'size ',n:3,m:3,chr(11),chr(11),chr(11)); first := FALSE END; FOR i1 := 1 TO n DO BEGIN FOR j1 := 1 TO m DO read(result_file, depth_count[i1,j1]); readln(result_file); END; IF n = m THEN process_eq_values(n,i,j) ELSE process_values(n,m,i,j); readln(result_file) END UNTIL ch = 't'; print_arcs(process_file); write(process_file,chr(11),' total = ',total); writeln(process_file); nm := n * m; writeln(' ',nodes:5,nm*pred(nm)DIV 2:5); END; BEGIN assign(result_file,'result.dat'); assign(process_file,'process4.doc'); reset(result_file); rewrite(process_file); WHILE NOT eof(result_file) DO process_one_size; close(process_file) END.