Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <sstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Ponto {
  10. public:
  11. int x;
  12. int y;
  13. int active;
  14. int sup;
  15. };
  16.  
  17. class Retangulo {
  18. public:
  19. int x;
  20. int y;
  21. int id;
  22. Ponto sup;
  23. Ponto inf;
  24. };
  25.  
  26. int nr_rets;
  27. int nr_configs = 0;
  28. int nr_rec = 0;
  29. int area = 0;
  30.  
  31. bool check_intersections(Retangulo r, vector<Retangulo> colocados){
  32. if (colocados.size() == 0 || colocados.size() == 1){
  33. return false;
  34. }
  35.  
  36. //Percorrer retangulos ja colocados
  37. for (int i=0; i<colocados.size(); i++){
  38.  
  39. if (r.sup.x < colocados.at(i).inf.x && r.inf.x > colocados.at(i).sup.x && r.sup.y > colocados.at(i).inf.y && r.inf.y < colocados.at(i).sup.y){
  40. //Existe interseção
  41. cout << "Intersetei";
  42. return true;
  43. }
  44. }
  45.  
  46. return false;
  47. }
  48.  
  49. bool check_retangle(vector<Retangulo> colocados, Retangulo r){
  50.  
  51. for (int i=0; i<colocados.size(); i++){
  52. if (r.id == colocados.at(i).id){
  53. //cout << "oi";
  54. return true;
  55. }
  56. }
  57.  
  58. return false;
  59. }
  60.  
  61. void mostra_pts(vector<Ponto> pts){
  62. int i;
  63. for(i=0;i<pts.size();i++){
  64. cout <<"X:" << pts.at(i).x << " Y:" << pts.at(i).y << " Active: "<< pts.at(i).active <<" \n";
  65. }
  66. cout << "\n";
  67. }
  68.  
  69. bool fitting_retangle(int x, int y){
  70. int area_to_fill = 0;
  71.  
  72. area_to_fill = x * y;
  73.  
  74. if(area < area_to_fill){
  75. return true;
  76. }
  77.  
  78. return false;
  79. }
  80.  
  81. bool is_retangle(vector<Ponto> pts){
  82. int area_ret;
  83. int x,y;
  84. vector<Ponto> active;
  85. Ponto p;
  86.  
  87. for (int i=0; i<pts.size(); i++){
  88. if (pts.at(i).active == 1){
  89. p.x = pts.at(i).x;
  90. p.y = pts.at(i).y;
  91. p.active = 1;
  92. active.push_back(p);
  93. }
  94. }
  95.  
  96. if (active.size() != 2){
  97. return false;
  98. }
  99. else{
  100. x = active.at(0).x + active.at(1).x;
  101. y = active.at(0).y + active.at(1).y;
  102. area_ret = x*y;
  103. }
  104.  
  105. if (area_ret == area){
  106. return true;
  107. }
  108.  
  109. return false;
  110. }
  111.  
  112. void recursive(Retangulo r, vector<Retangulo> rets, vector<Ponto> pts, vector<Retangulo> colocados, int x_max, int y_max){
  113.  
  114. int tam = pts.size();
  115. //Percorrer lista de cantos ativos
  116. for (int i=0; i<tam; i++){
  117. //Condição de paragem
  118. if (colocados.size() == nr_rets){
  119. //mostra_pts(pts);
  120. if (is_retangle(pts) == true){
  121. nr_configs++;
  122. }
  123. nr_configs++;
  124. return ;
  125. }
  126. if (pts.at(i).active == 1){
  127. Ponto p,p1;
  128.  
  129. pts.at(i).active = 0;
  130.  
  131. p.x = pts.at(i).x + r.x;
  132. p.y = pts.at(i).y;
  133.  
  134. p1.x = pts.at(i).x;
  135. p1.y = pts.at(i).y + r.y;
  136.  
  137.  
  138. if (!check_retangle(colocados,r)){
  139.  
  140. r.sup = p1;
  141. r.inf = p;
  142.  
  143. //Verificar se existem interseções entre o retangulo a colocar e os ja colocados
  144. //if (!check_intersections(r,colocados)){
  145. cout << "Coloquei o " << r.id << " no ponto " << pts.at(i).x << "," << pts.at(i).y << "\n";
  146.  
  147. colocados.push_back(r);
  148.  
  149. p1.active = 1;
  150. p.active = 1;
  151.  
  152. pts.push_back(p);
  153. pts.push_back(p1);
  154.  
  155. //update_active_pts(pts);
  156. //}
  157.  
  158. }
  159.  
  160. cout << "Colocados: ";
  161. for (int c = 0; c<colocados.size(); c++){
  162. cout << colocados.at(c).id << " ";
  163. }
  164. cout << "\n";
  165.  
  166. for (int j = 0; j<rets.size(); j++){
  167. if (!check_retangle(colocados,rets.at(j))){
  168. recursive(rets.at(j), rets, pts, colocados,0,0);
  169. }
  170. }
  171.  
  172. }
  173.  
  174. }
  175.  
  176. }
  177.  
  178. int main(){
  179. string line;
  180. string buf;
  181. int indices;
  182. int id = 1;
  183. int tam;
  184.  
  185. cin >> indices;
  186. nr_rets = indices;
  187.  
  188.  
  189. vector<Retangulo> rets;
  190. vector<Retangulo> colocados;
  191.  
  192. cin.ignore();
  193. while(indices != 0){
  194. getline(cin,line);
  195. Retangulo r;
  196.  
  197. stringstream ss(line);
  198. vector<string> tokens;
  199.  
  200. while (ss >> buf)
  201. tokens.push_back(buf);
  202.  
  203. if (!tokens.empty()){
  204. istringstream convert(tokens.at(0));
  205. convert >> r.x;
  206.  
  207. istringstream convert1(tokens.at(1));
  208. convert1 >> r.y;
  209. }
  210.  
  211. r.id = id;
  212.  
  213. rets.push_back(r);
  214.  
  215. id++;
  216. indices--;
  217. }
  218.  
  219. //Ponto na origem (0,0)
  220. vector<Ponto> pts;
  221. Ponto p;
  222. p.x = 0;
  223. p.y = 0;
  224. p.active = 1;
  225. pts.push_back(p);
  226.  
  227. //Rodar retangulos
  228. tam = rets.size();
  229. for (int i=0; i<tam; i++){
  230. if (rets.at(i).x != rets.at(i).y){
  231. //Nao e quadrado, logo podemos rodar
  232. Retangulo r1;
  233. r1.x = rets.at(i).y;
  234. r1.y = rets.at(i).x;
  235. r1.id = rets.at(i).id;
  236. rets.push_back(r1);
  237. }
  238. }
  239.  
  240. //Calculo da area total dos retangulos
  241. for (int i=0;i<rets.size();i++){
  242. area += (rets.at(i).x * rets.at(i).y);
  243. }
  244.  
  245. for (int i=0; i<rets.size(); i++){
  246. recursive(rets.at(i),rets,pts,colocados,0,0);
  247. }
  248.  
  249. cout << nr_configs;
  250.  
  251. return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement