type TForm1 = class(TForm) Memo1: TMemo; Memo2: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; PL:array of TPoint; //点集数组 TempDir,TempFile:string;//临时文件 ts1:TStringList; //记录已用过座标点集 ts2:TStringList; //记录当前座标点集
implementationvar xMax,xMin,yMax,yMin,ShortX,ShortY:Integer; //取得最短的X,Y{$R *.dfm} //判断数据是否在于数组中 function PointInPoint(p1:TPoint;p2:array of TPoint):Boolean; var x,y:Integer; begin Result:=False; for x := 0 to Length(p2) - 1 do begin if p1.X=p2[x].X then if p1.y=p2[x].y then begin Result:=true; Break; end; end;
end;//判断数据是否在于字符组中 function PointInTstr(p1:TPoint;ts1:TStringList):Boolean; var x,y:Integer; x2,y2,ps:Integer; Tstr:string; begin Result:=False; for x := 0 to ts1.Count - 1 do begin Tstr:=Trim(ts1.Strings[x]); ps:=pos(',',TStr); x2:=StrToInt(copy(TStr,0,ps-1)); y2:=StrToInt(copy(TStr,ps+1,Length(TStr)-ps));
if p1.X=x2 then if p1.y=y2 then begin Result:=true; Break; end; end;
end;//去掉重复 Function RemoveDuplicates(const stringList : TStringList):TStringList; var cnt: Integer; begin stringList.Sort; Result := TStringList.Create; Result.Sorted := True; Result.Duplicates := dupIgnore; Result.BeginUpdate; for cnt := 0 to stringList.Count - 1 do Result.Add(stringList[cnt]) ; Result.EndUpdate; end; //字符"3,5"转座标 x=3 y=5 function StrToXY(P1:String):TPoint ; var ps,x1,y1:Integer; begin Result.X:=0; Result.Y:=0;
result.X:=x1; Result.Y:=y1; end; //座标 x=3 y=5 转"3,5" function XYToStr(P1:TPoint):String ; begin result:=IntToStr(P1.X)+','+IntToStr(P1.Y); end;
{广泛搜索,将像索点的所有连接点检测出来} function CutPoint(dl: array of TPoint):TStringList; const direct :array [1..4,1..2] of shortint =((0,-1),(0,1),(-1,0),(1,0)); //方向增量 var i,j,k:Integer; //Star为队首指针,Ende为队尾指针 tmp2:TPoint;begin Result:=TStringList.Create; for j:=0 to Length(dl)-1 do //循环执行 begin //如果已加入队列,则跳过 if ts1.count>0 then if not PointInTstr(dl[j],ts1) then //如果不存在于数组中则跳过 continue;
for i:= 1 to 4 do //遍历四个方向 begin tmp2.X :=dl[j].X+direct[i,1]; tmp2.Y :=dl[j].Y+direct[i,2];
if (tmp2.X<0)or(tmp2.Y<0) then //如果越界则跳过 continue; if not PointInPoint(tmp2,dl) then //如果不存在于数组中则去跳过 continue else ts1.Add(IntToStr(tmp2.x)+','+IntToStr(tmp2.y));//将点集存入已用过点集 ts2.Add(IntToStr(tmp2.x)+','+IntToStr(tmp2.y));//将点集存入当前点集 //FillChar(dl,sizeof(dl[]),0); //初始化队列 end; end; Result:=RemoveDuplicates(ts2); //去掉重复 //Result.Free; //ts2.Free; end; procedure TForm1.Button1Click(Sender: TObject); var i,k,j:Integer; s1:string; ts3:TStringList; begin ts3:=TStringList.Create; SetLength(pl,memo1.Lines.Count); for I := 0 to memo1.Lines.Count - 1 do begin s1:=Trim(Memo1.Lines.Strings[i]); k:=Pos(',',s1); PL[i].X:=StrToInt(Copy(s1,0,k-1)); PL[i].Y:=StrToInt(Copy(s1,k+1,length(s1)-k)); end; for j := 0 to Length(PL)-1 do begin //Memo2.Text:=CutPoint(pl).GetText; ts3:=CutPoint(pl); ShowMessage(ts3.GetText); ts3.Clear;
end; ShowMessage(ts1.GetText);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin ts1.Free; ts2.Free; end;procedure TForm1.FormCreate(Sender: TObject); begin ts1 := TStringList.Create; ts2 := TStringList.Create; end;end.
是这样:
TStringlist A:如下:
8
9
8
11
12
14
18
23
25
30
35
....
现在,当这些字符串相加=50时(假设是一定有这个组合),
循环第一次,把8,12,30取出来存入Tstringlist B里面,
之后把8,12,30这几个从A里去掉..
然后再循环,把相加=50的再取出....
....以此类推..
但是一定要把这个字符串循环完,把所有的数据存出来...这样的结构怎么设计?
这个加=50,只是我打的比方的算法,因为真实的算法太大,没办法整上来,那个算法是从A中取出符合要求的行出来,然后再循环把剩下的符合要求的取出来,这个结构怎么设计?还是不清楚需求
试一试for list.count-1 downto 0 do。
这个加=50,只是我打的比方的算法,因为真实的算法太大,没办法整上来,那个算法是从A中取出符合要求的行出来,然后再循环把剩下的符合要求的取出来,这个结构怎么设计?还是不清楚需求
试一试for list.count-1 downto 0 do。
我把代码整出来吧,需求是这样的:
1.memo1存的是所有的座标点
2.把这些座标点相连的点取出来
3.循环取出所有的点代码如下:
unit Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TPixel =record //定义点类型
x,y:integer;
end;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
PL:array of TPoint; //点集数组
TempDir,TempFile:string;//临时文件
ts1:TStringList; //记录已用过座标点集
ts2:TStringList; //记录当前座标点集
implementationvar
xMax,xMin,yMax,yMin,ShortX,ShortY:Integer; //取得最短的X,Y{$R *.dfm}
//判断数据是否在于数组中
function PointInPoint(p1:TPoint;p2:array of TPoint):Boolean;
var
x,y:Integer;
begin
Result:=False;
for x := 0 to Length(p2) - 1 do
begin
if p1.X=p2[x].X then
if p1.y=p2[x].y then
begin
Result:=true;
Break;
end;
end;
end;//判断数据是否在于字符组中
function PointInTstr(p1:TPoint;ts1:TStringList):Boolean;
var
x,y:Integer;
x2,y2,ps:Integer;
Tstr:string;
begin
Result:=False;
for x := 0 to ts1.Count - 1 do
begin
Tstr:=Trim(ts1.Strings[x]);
ps:=pos(',',TStr);
x2:=StrToInt(copy(TStr,0,ps-1));
y2:=StrToInt(copy(TStr,ps+1,Length(TStr)-ps));
if p1.X=x2 then
if p1.y=y2 then
begin
Result:=true;
Break;
end;
end;
end;//去掉重复
Function RemoveDuplicates(const stringList : TStringList):TStringList;
var
cnt: Integer;
begin
stringList.Sort;
Result := TStringList.Create;
Result.Sorted := True;
Result.Duplicates := dupIgnore;
Result.BeginUpdate;
for cnt := 0 to stringList.Count - 1 do
Result.Add(stringList[cnt]) ;
Result.EndUpdate; end;
//字符"3,5"转座标 x=3 y=5
function StrToXY(P1:String):TPoint ;
var
ps,x1,y1:Integer;
begin
Result.X:=0;
Result.Y:=0;
ps:=pos(',',P1);
x1:=StrToInt(copy(P1,0,ps-1));
y1:=StrToInt(copy(P1,ps+1,Length(P1)-ps));
result.X:=x1;
Result.Y:=y1; end; //座标 x=3 y=5 转"3,5"
function XYToStr(P1:TPoint):String ;
begin
result:=IntToStr(P1.X)+','+IntToStr(P1.Y); end;
{广泛搜索,将像索点的所有连接点检测出来}
function CutPoint(dl: array of TPoint):TStringList;
const
direct :array [1..4,1..2] of shortint =((0,-1),(0,1),(-1,0),(1,0)); //方向增量
var
i,j,k:Integer; //Star为队首指针,Ende为队尾指针
tmp2:TPoint;begin
Result:=TStringList.Create; for j:=0 to Length(dl)-1 do //循环执行
begin
//如果已加入队列,则跳过
if ts1.count>0 then
if not PointInTstr(dl[j],ts1) then //如果不存在于数组中则跳过
continue;
for i:= 1 to 4 do //遍历四个方向
begin
tmp2.X :=dl[j].X+direct[i,1];
tmp2.Y :=dl[j].Y+direct[i,2];
if (tmp2.X<0)or(tmp2.Y<0) then //如果越界则跳过
continue;
if not PointInPoint(tmp2,dl) then //如果不存在于数组中则去跳过
continue
else
ts1.Add(IntToStr(tmp2.x)+','+IntToStr(tmp2.y));//将点集存入已用过点集
ts2.Add(IntToStr(tmp2.x)+','+IntToStr(tmp2.y));//将点集存入当前点集
//FillChar(dl,sizeof(dl[]),0); //初始化队列
end; end;
Result:=RemoveDuplicates(ts2); //去掉重复
//Result.Free;
//ts2.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,k,j:Integer;
s1:string;
ts3:TStringList;
begin
ts3:=TStringList.Create;
SetLength(pl,memo1.Lines.Count);
for I := 0 to memo1.Lines.Count - 1 do
begin
s1:=Trim(Memo1.Lines.Strings[i]);
k:=Pos(',',s1);
PL[i].X:=StrToInt(Copy(s1,0,k-1));
PL[i].Y:=StrToInt(Copy(s1,k+1,length(s1)-k));
end;
for j := 0 to Length(PL)-1 do
begin
//Memo2.Text:=CutPoint(pl).GetText;
ts3:=CutPoint(pl);
ShowMessage(ts3.GetText);
ts3.Clear;
end;
ShowMessage(ts1.GetText);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ts1.Free;
ts2.Free;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ts1 := TStringList.Create;
ts2 := TStringList.Create;
end;end.
1,2
1,3
0,3
2,2
2,3
2,6
2,7
3,6
3,7
2.把这些座标点相连的点取出来 -- 仅有一系列点,怎么算相连?
3.循环取出所有的点