Advertisement
Guest User

Depou

a guest
May 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. typedef struct mutare{char deUnde, unde; mutare *urm;};
  5. typedef struct nod{int info; nod *urm;};
  6. nod *stivaA=NULL, *stivaB=NULL;
  7. mutare *capMutari, *sfMutari;
  8. int u=0, nrMutari, i, n, x, ok = 1;
  9. void push(nod*&st,int x)
  10. {
  11. nod *p=new nod({x,st});
  12. st=p;
  13. }
  14. int pop(nod *&st)
  15. {
  16. int x=st->info;
  17. nod *p=st;
  18. st=st->urm;
  19. delete p;
  20. return x;
  21. }
  22. void add(char deUnde, char unde)
  23. {
  24. mutare *p = new mutare({deUnde, unde, NULL});
  25. nrMutari++;
  26. if(sfMutari == NULL)
  27. capMutari = p;
  28. else
  29. sfMutari->urm = p;
  30. sfMutari = p;
  31. }
  32. int main()
  33. {
  34. cin >> n;
  35. for(i=1; i<=n; i++){ cin >> x; push(stivaA,x); }
  36.  
  37. while (u < n && ok == 1)
  38. {
  39. // incerc sa mut din A in C
  40. if(stivaA != NULL && stivaA->info == u + 1)
  41. {
  42. u++;
  43. pop(stivaA);
  44. add('A','C');
  45. }
  46. else //incerc sa mut din B in C
  47. if(stivaB != NULL && stivaB->info == u + 1)
  48. {
  49. u++;
  50. pop(stivaB);
  51. add('B','C');
  52. }
  53. else // mut din A in B
  54. if(stivaA!=NULL)
  55. {push(stivaB,pop(stivaA));add('A','B');}
  56. else
  57. ok = 0;
  58. }
  59. nrMutari *= ok;
  60. cout << nrMutari <<"\n";
  61. if(nrMutari > 0)
  62. while(capMutari != NULL) { cout<<capMutari->deUnde<<" "<<capMutari->unde<<"\n";capMutari = capMutari->urm;}
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement