Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
Delphi XE3 Enterprise
|
AW: Felder,Ketten im Array finden
26. Jul 2011, 18:35
kleines demo
Delphi-Quellcode:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TInfoRec=Record
Sign:String;
Visited:Boolean;
End;
TInfoRecArray =Array of Array of TInfoRec;
TForm1 = class(TForm)
Button1: TButton;
g: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
FArray:TInfoRecArray;
FFound:Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function Count4Sign(arr:TInfoRecArray;const s:String;x,y:Integer):Integer;
begin
Result := 0;
if (x>=0) and (y>=0) and (x<=High(arr)) and (y<=High(arr[0]))then
if (arr[x,y].Sign = s) and not (arr[x,y].Visited) then
begin
Result := 1;
arr[x,y].Visited := true;
end;
if Result=1 then
begin
Result := Result + Count4Sign(arr,s,x + 1,y);
Result := Result + Count4Sign(arr,s,x - 1,y);
Result := Result + Count4Sign(arr,s,x,y + 1);
Result := Result + Count4Sign(arr,s,x,y - 1);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
x,y:Integer;
sign:String;
begin
SetLength(FArray,g.ColCount,g.RowCount);
sign := g.Cells[g.Col,g.Row];
For X := 0 to g.ColCount - 1 do
For Y := 0 to g.RowCount - 1 do
begin
FArray[x,y].Sign := g.Cells[x,y];
FArray[x,y].Visited := false;
end;
FFound := Count4Sign(FArray,sign,g.Col,g.Row);
Caption := IntToStr(FFound);
end;
Thomas Wassermann H₂♂ Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂♂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
|