1 / 7

Problema Comis Voiajorului

Problema Comis Voiajorului. Sikos Petra Clasa a XI-a B. Enunţul problemei.

amber
Télécharger la présentation

Problema Comis Voiajorului

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problema Comis Voiajorului Sikos Petra Clasa a XI-a B

  2. Enunţul problemei • Un comis voiajor trebuie să viziteze un numărdenoraşe.Iniţial acesta se aflăîntrunul dinele notate cu1.Eldoreşte să nu treacă de 2ori în acelaş oraş iar la întoarcere să seîntoarcăîn oraşul1.Cunoscînd legăturileexistente între oraşe , se cere toate drumurile posibile pe care le poate efectua.

  3. Exemplu Pentru n=6 avem: 1,2,3,4,5,6,11,2,5,4,3,6,11,6,3,4,5,2,11,6,5,4,3,2,1 4 2 3 1 6 5

  4. Algoritm • Nu sa mai trecut prin oraşul simbolizat de succesor adică nu se găseşte stiva elementului respectiv. • Ca să pot trece la oraşul următor trebuie ca să existe drum intre oraşul aflat pe nivelul K-1şi cel afla pe nivelul K. • Dacă succesorul se găseşte pe nivelul n atunci să existe drum de la el la oraşul 1.

  5. Implementare • Pentru rezolvarea problemei se utilizează stiva st unde st[k] reţine oraşele. • Avem o matrice patratică An,n. • A[I,j]=o dacă nu există drum între işi j. • A[I,j]=1 dacă există drum între işi j.

  6. Program comis_voiajorul; Type stiva=array [1..100] of integer; Var k,n,i:integer; St:stiva; As,ev:boolean; A : array [1..10,1..10] of integer; PROCEDURE INIT(k:integer; var st:stiva); Begin st[k]:=1; End; PROCEDURE SUCCESOR(var as:boolean; var st:stiva; k:integer); Begin If st[k]<n then begin as:=true; St[k]:=st[k]+1; end else as:=false; End; PROCEDURE VALID(var ev:boolea; st:stiva; k:integer); Begin ev:=true; If a[st[k-1], st[k]]=0 then ev:=false else for i:=1 to k-1 do if st[i]=st[k] then ev:=false else if (k=n) and (a[1,st[k]]=0) then ev:=false; End; Programul

  7. BEGINWrite(‘n=‘);readln(n);For i:=1 to n do For j:=1 to i-1 do begin Write(‘a[‘,i, ‘,’,j,’]=‘);readln(a[i,j]); a[j,i]:=a[i,j]; end;St[1]:=1;K:=2; init(k,st);While K > 0 do begin Repeat succesor(as,st,k); If as then valid(ev,st,k); Until (not as) OR (as and ev); If as then if solutie(k) then tipar else begin K:=K+1; init(k,st); end else K:=K-1;Readln;END. PROCEDURE VALID(var ev:boolea; st:stiva; k:integer); Begin ev:=true; If a[st[k-1], st[k]]=0 then ev:=false else for i:=1 to k-1 do if st[i]=st[k] then ev:=false else if (k=n) and (a[1,st[k]]=0) then ev:=false; End; PROCEDURE TIPAR; Begin For i:=1 to n do writeln(st[i]:3); writeln; End; END.

More Related