SHARE
TWEET

Untitled

a guest May 21st, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <math.h>
  3. int n,v[100],sol;
  4. void afisare()
  5. {int i,j,x;
  6.  sol++; std::cout<<"\n Solutia: "<<sol<<'\n';
  7.  for (i=1;i<=n;i++)
  8.  {for (j=1;j<=n;j++)
  9.  if (v[i]==j) std::cout<<"D ";
  10.  else std::cout<<"_ ";
  11.  std::cout<<'\n';
  12.  }
  13. }
  14. int valid(int k)
  15. {int i;
  16.  for (i=1;i<=k-1;i++)
  17.  if ((v[i]==v[k])||(abs(v[k]-v[i])==(k-i)))
  18.  return 0;
  19.  return 1;}
  20. int solutie(int k)
  21. {if (k==n)
  22.  return 1;
  23. return 0;}
  24. void BK(int k)
  25. {int i;
  26.  for (i=1;i<=n;i++)
  27.  {v[k]=i;
  28.  if (valid(k)==1)
  29.  {if (solutie(k)==1)
  30.  afisare();
  31.  else
  32.  BK(k+1);
  33.  }
  34.  }
  35. }
  36. int main()
  37. {std::cout<<"n= ";std::cin>>n;
  38.  BK(1);
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top