A
A
Alertoso2021-03-25 17:19:46
Delphi
Alertoso, 2021-03-25 17:19:46

How to convert recursion to stack?

procedure TForm1.BackTracking(k:integer;a:ta);
var i,j,o:integer;
Stack: array[1..8] of Integer;
po:boolean;
b:ta;
begin
if exitclick then exit;

repeat
if (K<Curlen) then
  Begin
    for i:=1 to curlen do
      b[i]:=Stack[i];
    for i:=1 to curlen do
    Begin
      po:=true;
      for j:=1 to k do
      Begin
        if Stack[j]=i then
          po:=false;
        if abs(Stack[j]-i)=abs(j-k-1) then
          po:=false
      End;
      if po then
      Begin
        b[k+1]:=i;
        Stack[o]:=b[k+1];
        inc(k);
        BackTracking(k+1,b);
      End;
    End;
  End
else
  Begin
    Inc(Co);
    Form1.Caption:=IntToStr(co);
    Form1.Draw(a);

    t:=GetTickCount;
    repeat
      Application.ProcessMessages;
    until (GetTickCount-t)>showtime;
    DrawGrid1.Repaint;
  End;
  inc(o);
Until o>curlen
End;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question