program matr; type
Matrix = array[1..8,1..8] of integer;
Algorithm = array[1..8,1..8] of integer;
var A:Matrix;
    B:Matrix;
    C:Matrix;
    M, N, K: integer;
    H, W: integer;
       Results:Text;
       MatrA: Text;
       MatrB: Text;
       MatrC: Text;
       Alg: Algorithm;

procedure ReadMatrix(var Matr:Matrix;i,j,rows,columns:integer; var F: Text);
 var
k,l:integer;
    begin
{Schitat iz faila F elementy matritsy v peremennuju Matr}
for k:=i to rows do
begin
for l:=j to columns do
begin
    Read (F,Matr[k][l]);
    end;
  end;
end;




procedure PrintMatrix(var Matr:Algorithm;i,j,rows,columns:integer;var F:Text); var
k,l:integer; begin
{Kazhdyj element matritsy Matr vyvesti na ekran2}
for k:=i to rows do
begin
for l:=j to columns do begin
Write (F,Matr[k][l], ' '); end;
WriteLn(F);end;end;



procedure InitZeros(var Matr: Matrix; i, j, rows, columns: integer); var
k,l:integer; begin
{Kazhdomu elementu matritsy Matr prisvoit 0}
for k:=i to rows do
begin
for l:=j to columns do begin
Matr[k][l]:=0; end; end; end;


function FindNextNonZero(Var Matr:Matrix; var startx,starty,rows,columns:integer):boolean;
var l:integer;
begin
l:= starty;
{Prosmatrivaem vse elementy v massive ���� ne najdem ne 0-j}
while ((Matr[startx][l] = 0) and (startx <= rows)) do
begin
while ((Matr[startx][l] = 0) and (l <= columns)) do
begin
l:=l+ 1;
end;
if Matr[startx][l] = 0 then begin
startx := startx + 1; l:=1; end; end;
starty := 1;
FindNextNonZero := (Matr[startx] [l]<>0); end;



procedure FindNextEmpty(startx:integer; var x, y: integer);
 var
l:integer;
begin
 l:=1;
{Prosmatrivaem vse elementy v massive ���� ne najdem pustuju stroku}
while (Length(Alg[startx][l]) <> 0) do
begin
if l=8 then
begin startx := startx + 1; l:=1;
end
else
begin
l:=l + 1;
end; end;
x := startx; y:=l;
  end;

function intToStr(I: Longint): String;
 { Convert any integer type to a string } var
S:string [32];
 begin
Str(I, S);
IntToStr:= S;
 end;

procedure ParseElement(i, j: integer);
 var l, v: integer;
  x_alg, y_alg: integer;
   begin l:=1;
while (I <= K) and ((A[i][l] = 0) or (B[l][j] = 0)) do
begin
l:=l+1; end;
if (A[i][l] <> 0) and (B[l][j] <> 0) then begin
FindNextEmpty (1, x_alg, y_alg);
Alg[x_alg][y_alg] := 'A' + IntToStr(i) + IntToStr(l) + '*B'+IntToStr(l)+IntToStr(j);
H := x_alg;
W := y_alg; end;
v:=l+1;
while (v <= K) and ((A[i][v] = 0) or (B[v][j] = 0)) do
begin
v:=v+1; end;

if (A[i][v] <> 0) and (B[v][j] <> 0) then
begin
FindNextEmpty (1, x_alg, y_alg);

Alg[x_alg][y_alg] := 'A' + IntToStr(i) + IntToStr(v) + '*B'+IntToStr(v)+intToStr(j);
FindNextEmpty (x_alg+l, x_alg, y_alg);
Alg[x_alg][y_alg] := 'A' + IntToStr(i) + IntToStr(l) + '*B'+IntToStr(l)+IntToStr(j)
+'+'+'A' + IntToStr(i) + IntToStr(v) + '*B'+IntToStr(v)+intToStr(j);

 H := x_alg;
 W := y_alg; end;
end;

procedure MakeAlg; var
 
i, j : integer;
 g, f: integer;
 begin i:=1; j:=1;
for g:= 1 to 8 do
 for f:= 1 to 8 do
  Alg[g][f]:=' ';
while (FindNextNonZero(C, i, j, M, K)) do
 begin
ParseElement(i, j);
if j = k then
begin
j:=i;
i:=i+1; end else
end; end;
begin
{Otkryt faily dlia chtenia ishodnyh matrits i dlia zapisi reultatov}
Assign(Results, 'results4.txt');
 Rewrite(Results);
 Assign(MatrA, 'A.txt');
 Assign(MatrB, 'B.txt');
 Assign(MatrC, 'C1.txt');
 reset(MatrA);
 Reset(MatrB);
 Reset(MatrC);
{Razmer matrits}
M:=5;N:=6; K:=7;
{Schitat matritsy A,B i C}
 ReadMatrix(A, 1,1, M, N, MatrA);
 ReadMatrix(B, 1,1, N, K, MatrB);
 ReadMatrix(C, 1,1, M, K, MatrC);
{Opredeliaem algoritm MakeAlg;}
{Vyvodim algoritm}
WriteLn (Results);
WriteLn (Results, 'Algorithm CO:');
PrintMatrix(Alg, 1,1,2, 8, Results);
 
WriteLn (Results);
WriteLn (Results, 'Xarakteristiki algoritma:');
WriteLn (Results);
WriteLn (Results, 'Sp = ', (8*(H-1) + W) / H);
WriteLn (Results);
WriteLn (Results, 'Ep = ', (8*(H-1) + W) / H / 8);
WriteLn (Results); WriteLn (Results, 'h = ', H);
WriteLn (Results);
WriteLn (Results, 'Z = ', (8*(H-1) + W) / (8*H) *100, '%');
{Zakryt faily} Close(MatrC); Close(MatrB); Close(MatrA);
Assign(MatrA, 'matr_al.txt'); Assign(MatrB, 'matrJM.txt'); Assign(MatrC, 'matr_cl.txt');
Reset(MatrA); Reset(MatrB); Reset(MatrC);
{Razmer matrits} M:=4;
N := 5; K:=4;
{Schitat matritsy A,B i C} InitZeros(A, 1,1, 8, 8); initZeros(B, 1,1, 8, 8);
InitZeros(C, 1, 1, 8, 8); ReadMatrix(A, 1,1, M, N, MatrA); ReadMatrix(B, 1,1, N, K, MatrB); ReadMatrix(C, 1, 1, M, K, MatrC);
{Opredeliaem algoritm} MakeAlg;
{Vyvodim algoritm}
WriteLn (Results);
WriteLn (Results, 'Algorithm CI:');
PrintMatrix(Alg, 1,1, 4, 8, Results);
WriteLn (Results);
WriteLn (Results, 'Xarakteristiki algoritma:');
WriteLn (Results);
 

writeln (Results, 'Sp = ', (8*(H-1) + W) / H);
WriteLn (Results);

WriteLn (Results, 'Ep = ', (8*(H-1) + W) / H / 8);
WriteLn (Results); WriteLn (Results, 'h = ', H);
WriteLn (Results);
WriteLn (Results, 'Z = ', (8*(H-1) + W) / (8*H) *100,'%');
{Zakryt faily} Close(MatrC); Close(MatrB); Close(MatrA);
Assign(MatrA, 'matr_a2.txt'); Assign(MatrB, 'matrjbj.txt'); Assign(MatrC, 'matr_c2.txt');
Reset(MatrA); Reset(MatrB); Reset(MatrC);
{Razmer matrits} M:=2;
N :=5; K:=4;
InitZeros(A, 1, 1, 8, 8); InitZeros(B, 1,1, 8, 8); InitZeros(C, 1,1, 8, 8);
{Schitat matritsy A,B i C}
 ReadMatrix(A, 1, 1, M, N, MatrA);
 ReadMatrix(B, 1,1, N, K, MatrB);
 ReadMatrix(C, 1, 1, M, K, MatrC);
{Opredeliaem algoritm} MakeAlg;
{Vyvodim algoritm}
WriteLn (Results);
WriteLn (Results, 'Algorithm C2:');
PrintMatrix(Alg, 1, 1,1, 8, Results);
WriteLn (Results);
WriteLn (Results, 'Xarakteristiki algoritma:');
WriteLn (Results);
WriteLn (Results, 'Sp = ', (8*(H-1) + W) / H);
WriteLn (Results);
WriteLn (Results, 'Ep = ', (8*(H-1) + W) / H / 8);
 
WriteLn (Results); WriteLn (Results, 'h = ', H);
WriteLn (Results);
WriteLn (Results, 'Z = ', (8*(H-1) + W) / (8*H) *100,'%');
{Zakryt faily} Close(MatrC); Close(MatrB); Close(MatrA);
Assign(MatrA, 'matr_a2.txt'); Assign(MatrB, 'matr_b0.txt'); Assign(MatrC, 'matr_c3.txt');
Reset(MatrA); Reset(MatrB); Reset(MatrC);
{Razmer matrits}
M:=2; N := 5; K:=2;
InitZeros(A, 1, 1, 8,8); InitZeros(B, 1,1, 8, 8); InitZeros(C, 1,1, 8, 8);
{Schitat matritsy A,B i Q ReadMatrix(A, 1, 1, M, N, MatrA); ReadMatrix(B, 1,1, N, K, MatrB); ReadMatrix(C, 1,1, M, K, MatrC);
{Opredeliaem algoritm} MakeAlg;
{Vyvodim algoritm}
WriteLn (Results);
WriteLn (Results, 'Algorithm C3:');
PrintMatrix(Alg, 1, 1,1, 8, Results);
WriteLn (Results);
WriteLn (Results, 'Xaraktenstiki algoritma:');
WriteLn (Results);
WriteLn (Results, 'Sp = ', (8*(H-1) + W) /H);
WriteLn (Results);
WriteLn (Results, 'Ep = ', (8*(H-1) + W) / H / 8);
WriteLn (Results); WriteLn (Results, 'h = ', H);
 
WriteLn (Results);
WriteLn (Results, 'Z = ', (8*(H-1) + W) / (8*H) *100,'%');
{Zakryt faily}
Close(MatrC);
Close(MatrB);
Close(MatrA);
Close(Results); end.