Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. /*se dau un numar nat n reprezentand un nr de persoane care sunt asezate pe n scaune sa se
  2. genereze toate modurile de generare ale pers pe scaune cu restrictile nicio persoana sa stea pe loc
  3. initial unde a stat si sa nu fi fost vecine si in permutarea initiala.*/
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int n,st[10000],k,a[100];
  8. long long contor=0;
  9.  
  10. void init()
  11. {
  12. st[k]=0;
  13. }
  14.  
  15. int succesor()
  16. {
  17. if(st[k]<n)
  18. {
  19. st[k]++;
  20. return 1;
  21. }
  22. return 0;
  23. }
  24.  
  25. int valid()
  26. {
  27.  
  28. for(int i=1; i<k; i++)
  29. if(st[i]==st[k])
  30. return 0;
  31. if(k>1){
  32. int j;
  33. for(j=1;j<=n;j++){
  34. if(st[k]==a[j]){
  35. if(k==j)
  36. return 0;
  37. break;
  38. }
  39. }
  40. if((a[j-1]==st[k-1] || a[j+1]==st[k-1]))
  41. return 0;
  42. }
  43. return 1;
  44. }
  45.  
  46. void tipar()
  47. {
  48. for(int i=1; i<=k; i++){
  49. cout<<st[i]<<" ";
  50. }
  51. cout<<endl;
  52. }
  53.  
  54. int sol()
  55. {
  56. return k==n;
  57. }
  58.  
  59. void back1()
  60. {
  61. int as;
  62. k=1;
  63. init();
  64. while(k>0)
  65. {
  66. as=succesor();
  67. while(as)
  68. {
  69. if(valid())
  70. {
  71. if(sol())
  72. tipar();
  73. else{
  74. k++;
  75. init();
  76. }
  77. }
  78. as=succesor();
  79. }
  80. k--;
  81. }
  82. }
  83.  
  84. int main()
  85. {
  86. cin>>n;
  87. for(int i=1;i<=n;i++){
  88. cin>>a[i];
  89. }
  90. back1();
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement