Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. using namespace std;
  5. ifstream f("date.in");
  6. ofstream g("date.out");
  7. int n,a[500][500]={0},aux[500],dl[]={-2,-1,1,2,2,1,-1,-2},dc[]={1,2,2,1,-1,-2,-2,-1};
  8. struct elem
  9. {int l,c;};
  10. elem sol[500];
  11. void citire()
  12. {
  13. cin >> n >> sol[1].l >> sol[1].c;
  14. }
  15. void init(int k)
  16. {
  17. aux[k]=-1;
  18. }
  19. int exista(int k)
  20. {
  21. if(aux[k]>7)
  22. return 0;
  23. return 1;
  24. }
  25. int cont(int k)
  26. {
  27. int i;
  28. for(i=1;i<=k-1;i++)
  29. if(sol[i].l==sol[k].l&&sol[i].c==sol[k].c)
  30. return 0;
  31. if(sol[k].l<1||sol[k].l>n||sol[k].c<1||sol[k].c>n)
  32. return 0;
  33. if(abs(sol[k].l-sol[k-1].l)+abs(sol[k].c-sol[k-1].c)!=3)
  34. return 0;
  35. return 1;
  36. }
  37. int solutie(int k)
  38. {
  39. if(k==n*n)
  40. return 1;
  41. return 0;
  42. }
  43. int tipar(int k)
  44. {
  45. int a[10][10]={0},i,j;
  46. for(i=1;i<=k;i++)
  47. a[sol[i].l][sol[i].c] = i;
  48. for(i=1;i<=n;i++)
  49. {for(j=1;j<=n;j++)
  50. g << a[i][j] << ' ';
  51. g << '\n';
  52. }
  53. g << '\n';
  54. }
  55. void valpos(int k)
  56. {
  57. sol[k].l=sol[k-1].l+dl[aux[k]];
  58. sol[k].c=sol[k-1].c+dc[aux[k]];
  59. }
  60. void bktg()
  61. { int k;
  62. k=2;
  63. init(k);
  64. while(k>1)
  65. if(exista(k))
  66. {
  67. aux[k]=aux[k]+1;
  68. valpos(k);
  69. if(cont(k)) if(solutie(k)) tipar(k);
  70. else {k++;init(k);}
  71. }
  72. else k--;
  73. }
  74. int main()
  75. {int i,j;
  76. citire();
  77. bktg();
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement