[Pascal] Programul clasic pentru metoda backtracking.

User avatar
Alexandru.B
Posts: 1391

[Pascal] Programul clasic pentru metoda backtracking.

Post#1 » July 13th, 2014, 4:20 pm

program backtrackking;

var
x:array[1..20] of integer;
n,k,i:integer;
ok:boolean;

function valid(k:integer):boolean;
var i:integer;
begin
valid:=true;
for i:=1 to k-1 do
if x[k]=x[i] then valid:=false;
end;

procedure tipar(n:integer);
var i:integer;
begin
for i:=1 to n do
write (x[i],' ');
writeln;
end;

begin
writeln('n=');read(n);
k:=2;x[1]:=1;

while k>0 do
begin
ok:=false;
while (x[k]<n) and (ok=false) do
begin
x[k]:=x[k]+1;
if valid(k) then ok:=true;
end;
if ok=false then k:=k-1
else if k=n then tipar(k)
else
begin
k:=k+1;
x[k]:=0;
end;
end;
end.
Daca vrei o viata perfecta pe Pronion , Staff-ul te roaga sa respecti Acest REGULAMENT

Image


Image

Return to “Pascal / Turbo Pascal”

Who is online

Users browsing this forum: No registered users and 1 guest

Pronion.Ro : Disclaimer
cron