Advertisement
Estera

pb bt in plan

Nov 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int s[16][16],n,v[16];
  4. void afisare()
  5. {
  6. for(int i=1;i<=n;i++)
  7. {
  8. for(int j=1;j<=n;j++)
  9. cout<<v[s[i][j]]<<' ';
  10. cout<<'\n';}
  11. cout<<'\n'<<'\n';
  12.  
  13. }
  14. bool conditie(int i,int j)
  15. {
  16. if(i==1 && j==1) return 1;
  17. if(i==1 && j>1) {if(v[s[i][j]]%2==v[s[i][j-1]]%2) return 1; else return 0;}
  18. if(i>2 && j==1) {if(v[s[i-1][j]]%2==v[s[i][j]]%2) return 1; else return 0;}
  19. if(i>2 && j>2) {if(v[s[i][j]]%2==v[s[i-1][j]]%2) return 1; else if(v[s[i][j]]%2==v[s[i][j-1]]%2) return 1;}
  20. return 0;
  21. }
  22. void backtr(int i,int j)
  23. {
  24. int x;
  25. if(i>0 && i<=n && j>0 && j<=n)
  26. {
  27. for(x=1;x<=n*n;x++)
  28. {
  29. s[i][j]=x;
  30. if(conditie(i,j)) if(i==n && j==n) afisare();
  31. else if(j<n) backtr(i,j+1);
  32. else backtr(i+1,1);
  33. }
  34. }
  35. }
  36. int main()
  37. {
  38. cin>>n;
  39. for(int i=1;i<=n*n;i++)
  40. cin>>v[i];
  41. backtr(1,1);
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement