Advertisement
Maria_Teodora

Lant1

Jan 22nd, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f ("lant1.in");
  6. ofstream g ("lant1.out");
  7.  
  8. int a[101][101],n,p,q,r,st[101];
  9.  
  10. void citire()
  11. {
  12.     f>>n>>p>>q>>r;
  13.     int x,y;
  14.     while(f>>x>>y)
  15.     {
  16.         a[x][y]=a[y][x]=1;
  17.     }
  18. }
  19.  
  20. bool solutie(int k)
  21. {
  22.     int s=0;
  23.     for(int i=1;i<=k && s==0;i++)
  24.         if(st[i]==r)
  25.             s=1;
  26.     if(st[k]==q && s)
  27.         return 1;
  28.     else return 0;
  29. }
  30.  
  31. void afisare(int k)
  32. {
  33.     for(int i=1;i<=n;i++)
  34.         g<<st[i]<<" ";
  35. }
  36.  
  37. int dfs(int w)
  38. {
  39.     int k=2;
  40.     st[1]=w;
  41.     while(k>1 && k<=2*n)
  42.     {
  43.         int ok=0;
  44.         for(int i=1;i<=n && ok==0;i++)
  45.             if(a[i][st[k]]==1 && st[k]!=i && st[k-1]!=i)
  46.                 ok=1,st[++k]=i;
  47.         if(solutie(k))
  48.             {
  49.             afisare(k);
  50.             return 0;
  51.             }
  52.         else
  53.             if(st[k]==q)
  54.                 st[k--]=0; /*banuiesc ca problema este atunci cand
  55.                     coboara un nivel si intra pe exact aceleasi cazuri. Nu pot insa sa folosesc
  56.                     un vector viz[] deoarece se precizeaza ca lantul nu este elementar*/
  57.     }
  58. }
  59.  
  60. int main()
  61. {
  62.     citire();
  63.     dfs(p);
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement