Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct Result{
  6. int x;
  7. int y;
  8. int size;
  9. };
  10. int n, m;
  11. int minCount;
  12. int colorings = 0;
  13. vector<Result> minRect;
  14.  
  15.  
  16. void clear_rect(int*** rect){
  17. for (int i = 0; i < n; i++)
  18. for (int j = 0; j < m; j++){
  19. (*rect)[i][j] = 0;
  20. }
  21. }
  22. //Проверяем, можно ли вставить квадрат размера size в точку {x,y}
  23. bool isPushable(int** rect, int x, int y, int size){
  24. if (n-x < size || m-y < size)
  25. return false;
  26. for (int i = x; i < x+size; i++)
  27. for (int j = y; j < y+size; j++){
  28. if (rect[i][j])
  29. return false;
  30. }
  31. return true;
  32. }
  33.  
  34. void push(int*** rect, int x, int y, int size){
  35. for (int i = x; i < x+size; i++)
  36. for (int j = y; j < y+size; j++){
  37. (*rect)[i][j] = size;
  38. }
  39. }
  40.  
  41. void rec_back(int*** rect, int curSquare, int unPainted, int countOfSquares, vector<Result>* res){
  42. if (unPainted < curSquare*curSquare)
  43. return;
  44.  
  45. int** _rect = new int*[n];
  46.  
  47. for (int i = 0; i < n; i++) {
  48. _rect[i] = new int[m];
  49. copy((*rect)[i], (*rect)[i]+m, _rect[i]);
  50. }
  51.  
  52. vector<Result>* _res = new vector<Result>;
  53. _res->assign(res->begin(), res->end());
  54. bool pushed = false;
  55. for (int i = 0; i < n && !pushed; i++){
  56. for (int j = 0; j < m && !pushed; j++){
  57. if (_rect[i][j] == 0) {
  58. if (isPushable(_rect, i, j, curSquare)) {
  59. pushed = true;
  60. push(&_rect, i, j, curSquare);
  61. _res->push_back({i, j, curSquare});
  62. }
  63. else
  64. return;
  65. }
  66. else
  67. j+=_rect[i][j]-1;
  68. }
  69. }
  70.  
  71. if (pushed){
  72. if (countOfSquares+1 == minCount) {
  73. if (unPainted!=curSquare*curSquare) {
  74. return;
  75. } else {
  76. colorings++;
  77. return;
  78. }
  79. }
  80. if (unPainted==curSquare*curSquare && countOfSquares+1 < minCount){
  81. colorings = 1;
  82. minCount = countOfSquares+1;
  83. minRect.assign(_res->begin(), _res->end());
  84. return;
  85. }
  86. for (int i = min(min(n,m)-1, max(n,m) - curSquare); i > 0; i--)
  87. rec_back(&_rect, i, unPainted-curSquare*curSquare, countOfSquares+1, _res);
  88. }
  89. else {
  90. for (int i = curSquare-1; i > 0; i--)
  91. rec_back(&_rect, i, unPainted, countOfSquares, _res);
  92. }
  93.  
  94. for (int i = 0; i < n; i++) {
  95. delete _rect[i];
  96. }
  97. delete[] _rect;
  98. delete _res;
  99. }
  100.  
  101. int main() {
  102. cout << "Enter rectangle height&width: ";
  103. cin >> n >> m;
  104. minCount = n*m;
  105. int** rect = new int*[n];
  106. for (int i = 0; i < n; i++)
  107. rect[i] = new int[m];
  108. clear_rect(&rect);
  109. vector<Result> vec;
  110. for (int i = min(n,m)-1; i > 0; i--)
  111. rec_back(&rect, i, n*m, 0, &vec);
  112. cout << "Minimal number of squares: " << minCount << endl;
  113. for (auto i = minRect.begin(); i != minRect.end(); i++){
  114. cout << i->x << ' ' << i->y << ' ' << i->size << '\n';
  115. }
  116. cout << "\nCount of minimal permutations: " << colorings << endl;
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement