Advertisement
a53

ornament

a53
Jul 28th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. //Marinel Serban ianuarie 2015
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin ("ornament.in");
  9. ofstream fout("ornament.out");
  10.  
  11. long k, l, m, n;
  12. long a[18][18], b[18][18];
  13. int luate[20], CeFac;
  14. long long sol = 0;
  15.  
  16. void scrie()
  17. {
  18. int i, j;
  19. for (i = 1; i <= k; i++)
  20. {
  21. for (j = 1; j <= k; j++)
  22. fout << a[i][j] << ' ';
  23. fout << '\n';
  24. }
  25. //fout << '\n';
  26. fout.close();
  27. }
  28.  
  29. void back(long l, long c)
  30. {
  31. long p;
  32. if (l == k + 1) //am umplut k linii
  33. {
  34. if (CeFac == 1)
  35. {
  36. scrie();
  37. exit(0);
  38. }
  39. sol++;
  40. }
  41. else
  42. for (p = 1; p <= n; ++p) //parcurg cele n patratele
  43. if (!luate[p]) //daca nu l-am luat inca
  44. if (l == 1 || b[p][1] == b[a[l-1][c]][3])
  45. if (c == 1 || b[p][4] == b[a[l][c-1]][2])
  46. { //se potriveste
  47. luate[p] = 1; //l-am luat
  48. a[l][c] = p; //il pun la locul lui
  49. if (c == k) //daca am umplut linia
  50. back(l+1, 1); //trec la linie noua
  51. else //altfel
  52. back(l, c+1); //trec in aceiasi linie mai departe
  53. a[l][c] = 0; //iau patratelul
  54. luate[p] = 0; //
  55. }
  56. }
  57.  
  58. void Rezolva()
  59. {
  60. back(1, 1); //plec de la prima pozitie
  61. }
  62.  
  63. void ReadData()
  64. {
  65. int i;
  66. fin >> n;
  67. k = sqrt(n); //atata e k
  68. for (i = 1; i <= n; i++)
  69. fin >> b[i][1] >> b[i][2] >> b[i][3] >> b[i][4];
  70. fin >> CeFac;
  71. Rezolva();
  72. if (sol == 0)
  73. fout << -1 << '\n'; //nu e cazul dar...
  74. else
  75. fout << sol << '\n';
  76. fout.close();
  77. }
  78.  
  79. int main()
  80. {
  81. ReadData();
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement