Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*se dau un numar nat n reprezentand un nr de persoane care sunt asezate pe n scaune sa se
- genereze toate modurile de generare ale pers pe scaune cu restrictile nicio persoana sa stea pe loc
- initial unde a stat si sa nu fi fost vecine si in permutarea initiala.*/
- #include <iostream>
- using namespace std;
- int n,st[10000],k,a[100];
- long long contor=0;
- void init()
- {
- st[k]=0;
- }
- int succesor()
- {
- if(st[k]<n)
- {
- st[k]++;
- return 1;
- }
- return 0;
- }
- int valid()
- {
- for(int i=1; i<k; i++)
- if(st[i]==st[k])
- return 0;
- if(k>1){
- int j;
- for(j=1;j<=n;j++){
- if(st[k]==a[j]){
- if(k==j)
- return 0;
- break;
- }
- }
- if((a[j-1]==st[k-1] || a[j+1]==st[k-1]))
- return 0;
- }
- return 1;
- }
- void tipar()
- {
- for(int i=1; i<=k; i++){
- cout<<st[i]<<" ";
- }
- cout<<endl;
- }
- int sol()
- {
- return k==n;
- }
- void back1()
- {
- int as;
- k=1;
- init();
- while(k>0)
- {
- as=succesor();
- while(as)
- {
- if(valid())
- {
- if(sol())
- tipar();
- else{
- k++;
- init();
- }
- }
- as=succesor();
- }
- k--;
- }
- }
- int main()
- {
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>a[i];
- }
- back1();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement