Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Stack overflow

$
0
0
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.
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;


Viewing all articles
Browse latest Browse all 2703

Trending Articles