Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Stack overflow

autoriusautorius Posts: 2Member
So, i have written recursion which after certain number of calls causes stack overflow. I would like to know if somehow i can avoid this.
[code]
procedure SOLVE (var A : LBOARD; INDEX : integer; var SOLUTION : boolean);
var i : integer;
begin
if SOLUTION = false
then if FULL(A)
then begin
SOLUTION := true;
TextColor(10);
WriteLn('Possible solution found.');
PRINT_CHESSBOARD(A);
end
else if THERE_ARE_POSSIBLE_MOVES(A)
then begin
for i := INDEX downto 1 do
if CAN_BE_MOVED(A, i) and (A[i] = 1)
then begin
MOVE(A, i);
RESET(A, i);
if QUEEN_TO_LEFT(A, i) = 0
then SOLVE(A, QUEEN_TO_RIGHT(A), SOLUTION)
else SOLVE(A, QUEEN_TO_LEFT(A,i), SOLUTION);
end;
end
else begin
TextColor(4);
WriteLn('No possible solution found.');
end;
end;
[/code]

Comments

Sign In or Register to comment.