1 / 12

In cautarea comorii ~comis voiajor~

In cautarea comorii ~comis voiajor~. Mancu Oana. Vandra Steliana. abcdefghijk6. Mota Alin. Metoda backtracking este folosita de majoritatea programatorilor in cazul in care nu dispun de o metoda mai rapida de rezolvare pentru generarea tuturor solutiilor unei probleme.

studs
Download Presentation

In cautarea comorii ~comis voiajor~

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. In cautarea comorii~comis voiajor~ Mancu Oana Vandra Steliana abcdefghijk6 Mota Alin

  2. Metoda backtracking este folosita de majoritatea programatorilor in cazul in care nu dispun de o metoda mai rapida de rezolvare pentru generarea tuturor solutiilor unei probleme

  3. Incercand sa rezolvam aceasta problema, ne dam seama cum sa aplicam metoda de backtracking pentru a afla toate posibilitatile si apoi sa o alegem pe cea mai buna pentru rezolvarea unei probleme din viata de zi cu zi.

  4. Ajuta-l pe pirat sa gaseasca toate bucatile de harta, care se afla in locuri diferite, pentru a afla unde este comoara inaintea celorlalti pirati. La inceput echipajul este odihnit si poate face drumurile lungi si grele. Insa, la sfarsit, el oboseste si prefera sa parcurga drumurile mai usoare. In concluzie solutia gasita trebuie sa fie cea mai convenabila pentru echipaj

  5. In exemplul urmator vom lua 5 insule intre care exista 8 legaturi: 1(A)-2(B) 1(A)-3(C) E(5) 1(A)-4(D) 1(A)-5(E) A(1) 2(B)-3(C) 3(C)-4(D) 3(C)-5(E) D(4) 4(D)-5(E) C(3) B(2)

  6. Cum rezolvam problema? Mai intai generam toate variantele posibile folosind programul: int solutie(int k) {return k==n+1;} void tipar() {int i; for(i=1;i<=n;i++) cout<<st[i]; cout<<endl; } void back(int k) {if (solutie(k))tipar(); else{init(k); while(succesor(k)) if (valid(k)) back(k+1); }} main() {int m,x,y,i; clrscr(); cout<<"nr orase";cin>>n; cout<<"legaturi";cin>>m; for(i=1;i<=m;i++){cin>>x>>y; a[x][y]=1; a[y][x]=1; } back(1); getch(); void init(int k) {st[k]=0; } int succesor(int k) {if (st[k]<n) {st[k]++; return 1; } else return 0;} int valid(int k) {int i; for(i=1;i<=k-1;i++) if(st[k]==st[i])return 0; if(k>1)if(a[st[k]][st[k-1]]==0)return 0; if(k==n && a[st[k]][st[1]]==0)return 0; return 1;} int solutie(int k) {return k==n+1; }} Apasa pe timona

  7. In urma inserarii datelor vom obtine urmatorul tabel sub forma de matrice, unde avem reprezentata cu 1 existenta legaturilor dintre insulele de pe orizontala si cele de pe verticala X1 X2 X3 X4 X5 A B C D E 0 1 1 1 1 Y1 A 1 0 1 0 0 Y2 B 1 1 0 1 0 Y3 C 1 0 1 0 1 Y4 D Y5 E 1 0 0 1 0 Legaturile erau: A-B, A-C, A-D, A-E, B-C, C-D, D-E

  8. solutie 0 1 K St[k] 1 succesor 5 0 1 2 3 4 5 1 0 valid 4 0 1 2 3 4 Solutii 3 0 1 2 3 12345 (ABCDE) 2 0 1 2 Pt aceasta solutie piratul va face urmatorul drum: 1 0 1

  9. A B E C D

  10. Dupace am generattoatedrumurilecautamceamaibunasolutiesi o afisam, sa vedemdacagasimcomoara Apasa aici

  11. Ce am invatat: • Prin lucrul in echipa, putem aprofunda informatica,intr-un mod placut.

  12. Bibliografie • Manual informatica • http://garaj.xhost.ro • http://worldit.info

More Related