unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls, Buttons, Mask, ExtCtrls; type TForm1 = class(TForm) StringGrid1: TStringGrid; BitBtn1: TBitBtn; BitBtn2: TBitBtn; RadioGroup1: TRadioGroup; Label1: TLabel; BitBtn3: TBitBtn; Edit1: TEdit; Button1: TButton; procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure BitBtn3Click(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type znach = record number:integer; priznak:char; end; var Form1: TForm1; matrix:array[0..200,0..200]of znach; mass:array[0..200,0..200]of integer; n:integer; implementation {$R *.dfm} procedure Xmax(); var i,j,max:integer; begin max:=matrix[1,1].number; for i:=1 to n-1 do for j:=1 to n-1 do if max'|')and(matrix[i,count].priznak<>'*') then begin inc(kol); flag:=i; end; if (kol<>0)and(kol<2)and(matrix[flag,count].priznak<>'*')and(matrix[flag,count].priznak<>'|') then begin matrix[flag,count].priznak:='*'; for j:=1 to n-1 do if (matrix[flag,j].number=0)and(matrix[flag,j].priznak<>'*') then matrix[flag,j].priznak:='|'; // ShowMessage(IntToStr(Flag)+' '+IntToStr(Count)); end; flag:=0; inc(count); end; flag:=0; count:=1; While(count'|')and(matrix[count,i].priznak<>'*') then begin inc(kol); flag:=i; end; if (kol<>0)and(kol<2)and(matrix[count,flag].priznak<>'*')and(matrix[count,flag].priznak<>'|') then begin matrix[count,flag].priznak:='*'; for j:=1 to n-1 do if (matrix[j,flag].number=0)and(matrix[j,flag].priznak<>'*') then matrix[j,flag].priznak:='|'; //ShowMessage(IntToStr(Flag)+' '+IntToStr(Count)); end; flag:=0; inc(count); end; end; Procedure Shag_Three(); var i,j,flag,count:integer; begin count:=1; flag:=0; While(count'+') then if min>matrix[i,j].number then min:=matrix[i,j].number; // ShowMessage(IntToStr(min)); for i:=1 to n-1 do for j:=1 to n-1 do if (matrix[0,j].priznak='+')and(matrix[i,0].priznak<>'+') then matrix[i,j].number:=matrix[i,j].number-min; for i:=1 to n-1 do for j:=1 to n-1 do if (matrix[0,j].priznak<>'+')and(matrix[i,0].priznak='+') then matrix[i,j].number:=matrix[i,j].number+min; end; function find_zero():integer; var i,j,kol:integer; begin kol:=0; for i:=1 to n-1 do for j:=1 to n-1 do if (matrix[i,j].number=0)and(matrix[i,j].priznak<>'|')and(matrix[i,j].priznak<>'*') then inc(kol); if kol>0 then find_zero:=1 else find_zero:=0; end; function finish():integer; var i,j,kol:integer; begin kol:=0; for i:=1 to n-1 do for j:=1 to n-1 do if matrix[i,j].priznak='*' then inc(kol); if kol=n-1 then finish:=1 else finish:=0; end; Procedure PClear(); var i,j:integer; begin for i:=0 to n-1 do for j:=0 to n-1 do matrix[i,j].priznak:=' '; end; procedure TForm1.BitBtn1Click(Sender: TObject); var i,j,konec,rez:integer; begin for i:=1 to n-1 do for j:=1 to n-1 do begin matrix[i,j].number:=StrToInt(StringGrid1.Cells[i,j]); mass[i,j]:=StrToInt(StringGrid1.Cells[i,j]); end; konec:=1; if RadioGroup1.Buttons[0].Checked=true then Xmax(); Shag_one(); PClear(); While(konec=1)do begin while(find_zero()=1)do Shag_two(); if finish()=1 then break; Shag_three(); Shag_four(); PClear(); end; rez:=0; for i:=1 to n-1 do for j:=1 to n-1 do if matrix[i,j].priznak='*' then rez:=rez+mass[i,j]; ShowMessage('Результат: '+IntToStr(rez)); end; procedure TForm1.BitBtn2Click(Sender: TObject); var i,j:integer; begin for i:=1 to n-1 do for j:=1 to n-1 do if matrix[i,j].priznak='*' then StringGrid1.Cells[i,j]:=matrix[i,j].priznak else StringGrid1.Cells[i,j]:=intToStr(matrix[i,j].number); end; procedure TForm1.FormCreate(Sender: TObject); begin RadioGroup1.Buttons[0].Checked:=true; n:=StrToInt(Edit1.Text)+1; StringGrid1.RowCount:=StrToInt(Edit1.Text)+1; StringGrid1.ColCount:=StrToInt(Edit1.Text)+1; end; procedure TForm1.BitBtn3Click(Sender: TObject); var i,j:integer; begin if Edit1.Text='' then begin ShowMessage('Не заполнена размерность матрицы'); exit; end; n:=StrToInt(Edit1.Text)+1; StringGrid1.RowCount:=StrToInt(Edit1.Text)+1; StringGrid1.ColCount:=StrToInt(Edit1.Text)+1; for i:=1 to StrToInt(Edit1.Text) do for j:=1 to StrToInt(Edit1.Text) do if StringGrid1.Cells[i,j]='' then begin ShowMessage('В матрице заполнены не все поля'); Exit; end; BitBtn1.Enabled:=true; BitBtn2.Enabled:=true; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not(key in ['0'..'9',#8]) then key:=#0; end; procedure TForm1.Button1Click(Sender: TObject); begin n:=StrToInt(Edit1.Text)+1; StringGrid1.RowCount:=StrToInt(Edit1.Text)+1; StringGrid1.ColCount:=StrToInt(Edit1.Text)+1; end; end.