Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.14 KB | None | 0 0
  1. #include <vector>
  2. #include <cstdio>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct rectangle{
  9.     int width,height;
  10.     bool horizontal,vertical,placed,first_placed_vertical,first_placed_horizontal;
  11.     int urc [2],ulc[2],brc[2],blc[2];
  12.  
  13.  
  14. }Rectangle;
  15.  
  16. typedef struct point{
  17.     int x,y,visited;
  18.  
  19. }Point;
  20.  
  21. bool check_intersection(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec,int orientation);
  22. bool place_rectangle(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec);
  23. int n_rectangles=0; /*nº de rectangulos no input*/
  24. long total_height=0,total_width=0;
  25. long area=0;
  26. bool solution(Rectangle *array_rec,vector<Rectangle> &array_col,int*solutions, long* area,vector<Point> &possible_points,vector <Point> &removed_points,int rec);
  27. int calls=0;
  28. int main() {
  29.  
  30.     int solutions=0;
  31.     Rectangle* array_rec=new Rectangle[n_rectangles]; /* array que guarda as medidas de cada rectangulo*/
  32.     vector<Rectangle> array_col;
  33.     vector <Point> possible_points;
  34.     vector <Point> removed_points;
  35.     scanf("%d",&n_rectangles);
  36.     for(int i=0;i<n_rectangles;i++){
  37.         fscanf(stdin,"%d %d",&array_rec[i].width,&array_rec[i].height);
  38.         array_rec[i].placed=false;
  39.         array_rec[i].first_placed_vertical=false;
  40.         array_rec[i].first_placed_horizontal=false;
  41.         area += (array_rec[i].width * array_rec[i].height);
  42.     }
  43.     printf("Área Total:%ld\n",area);
  44.     solution(array_rec,array_col,&solutions,&area,possible_points,removed_points,0);
  45.     printf("%d",solutions);
  46.     return 0;
  47. }
  48.  
  49. void vertical_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  50.     array_rec[sq].blc[0] = blcx;
  51.     array_rec[sq].blc[1] = blcy;
  52.     array_rec[sq].brc[0] = blcx+array_rec[sq].width;
  53.     array_rec[sq].brc[1] = blcy;
  54.     array_rec[sq].ulc[0] = blcx;
  55.     array_rec[sq].ulc[1] = blcy+array_rec[sq].height;
  56.     array_rec[sq].urc[0] = blcx+array_rec[sq].width;
  57.     array_rec[sq].urc[1] = blcy+array_rec[sq].height;
  58. }
  59.  
  60. void horizontal_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  61.     array_rec[sq].blc[0] = blcx;
  62.     array_rec[sq].blc[1] = blcy;
  63.     array_rec[sq].brc[0] = blcx+array_rec[sq].height;
  64.     array_rec[sq].brc[1] = blcy;
  65.     array_rec[sq].ulc[0] = blcx;
  66.     array_rec[sq].ulc[1] = blcy+array_rec[sq].width;
  67.     array_rec[sq].urc[0] = blcx+array_rec[sq].height;
  68.     array_rec[sq].urc[1] = blcy+array_rec[sq].width;
  69. }
  70.  
  71. bool accept_solution(Rectangle* array_rec){
  72.  
  73.     for(int i=0;i<n_rectangles;i++){
  74.         if(!array_rec[i].placed){
  75.             return false;
  76.         }
  77.     }
  78.     return true;
  79. }
  80.  
  81. bool place_rectangle(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec){
  82.  
  83.     for(int x =0; x<(int)possible_points.size();x++){
  84.         if (possible_points[x].x==toplace.x && possible_points[x].y==toplace.y){
  85.             possible_points[x].visited+=1;
  86.         }
  87.     }
  88.     toplace.visited+=1;
  89.     printf("Rectangulo: %d\n",rec);
  90.     Rectangle aux_rec=array_rec[rec];
  91.     vertical_coordinates(array_rec, rec, toplace.x, toplace.y);
  92.     printf("To Place: %d %d\n", toplace.x, toplace.y);
  93.     if(check_intersection(array_rec,possible_points,toplace,rec,0)) {
  94.         Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1],0};
  95.         Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1],0};
  96.         printf("Point ulc: %d %d Point brc: %d %d \n", ulc.x, ulc.y, brc.x, brc.y);
  97.         possible_points.push_back(ulc);
  98.         possible_points.push_back(brc);
  99.         printf("ADICIONAMOS PONTO\n");
  100.         printf("ADICIONAMOS PONTO\n");
  101.  
  102.         return true;
  103.     }
  104.     else {
  105.         printf("rodou\n");
  106.         printf("To Place Horiz: %d %d\n", toplace.x, toplace.y);
  107.         horizontal_coordinates(array_rec,rec,toplace.x,toplace.y);
  108.         if(check_intersection(array_rec,possible_points,toplace,rec,1)){
  109.  
  110.             Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1],0};
  111.             Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1],0};
  112.             printf("Pontos a inserir possible ulc: %d %d Point brc: %d %d \n", ulc.x, ulc.y, brc.x, brc.y);
  113.             possible_points.push_back(ulc);
  114.             possible_points.push_back(brc);
  115.             return true;
  116.         }
  117.         array_rec[rec]=aux_rec;
  118.         return false;
  119.     }
  120. }
  121. bool check_intersection(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec,int orientation){ /* orientation: 0 vertical 1 horizontal*/
  122.     if (toplace.x == 0 && toplace.y==0) {
  123.         total_width = array_rec[rec].width;
  124.         total_height = array_rec[rec].height;
  125.         return true;
  126.     }
  127.     if(toplace.y==total_height){ /*em cima */
  128.         if(toplace.x+array_rec[rec].width>total_width){
  129.             printf("TO y %d wid %d  total%d\n",toplace.x,array_rec[rec].width,total_width);
  130.  
  131.             printf("FALSE 1\n");
  132.             return false;
  133.         }
  134.     }
  135.     else{ /*de lado*/
  136.         if(toplace.x == total_width) {
  137.             if (toplace.y + array_rec[rec].height > total_height) {
  138.                 printf("TO y %d height%d ",toplace.y,array_rec[rec].height);
  139.                 printf("FALSE 2\n");
  140.                 return false;
  141.             }
  142.         }
  143.     }
  144.  
  145.     long auxtotal_width=total_width;
  146.     long auxtotal_height=total_height;
  147.     if(total_height<array_rec[rec].ulc[1]){
  148.         auxtotal_height=array_rec[rec].ulc[1];
  149.     }
  150.  
  151.     if(total_width<array_rec[rec].brc[0]) {
  152.         auxtotal_width =array_rec[rec].brc[0];
  153.     }
  154.     printf("Largura Total: %ld Altura Total: %ld\n",auxtotal_width,auxtotal_height);
  155.     if((auxtotal_width)*auxtotal_height<=area){
  156.         total_width=auxtotal_width;
  157.         total_height=auxtotal_height;
  158.         return true;
  159.     }
  160.     else{
  161.  
  162.         return false;
  163.     }
  164.  
  165. }
  166.  
  167. bool check_all_placed(Rectangle *array_rec){
  168.     int placed=0;
  169.     for(int i=0;i<n_rectangles;i++){
  170.         if(array_rec[i].placed){
  171.             placed++;
  172.         }
  173.     }
  174.     if(placed==n_rectangles){
  175.         return true;
  176.     }
  177.     else{
  178.         return false;
  179.     }
  180. }
  181.  
  182. bool all_rectangle_tested(Point point,vector<Rectangle> &array_col){
  183.     printf("Visitados do ponto %d %d: %d col%d\n",point.x,point.y,point.visited,(int)array_col.size());
  184.     if(point.visited==(n_rectangles-(int)array_col.size())){
  185.         printf("ALL TESTED TRUE %d %d\n",point.x,point.y);
  186.  
  187.         return  true;
  188.     }
  189.     printf("ALL FALSE %d %d\n",point.x,point.y);
  190.  
  191.     return false;
  192.  
  193. }
  194. bool solution(Rectangle *array_rec,vector<Rectangle> &array_col,int*solutions, long* area,vector<Point> &possible_points,vector <Point> &removed_points,int rec) {
  195.  
  196.  
  197.     printf("inicio da func rectangulo %d\n", rec);
  198.     if (check_all_placed(array_rec)) { /*Solução encontrada*/
  199.         *solutions += 1;
  200.         printf("################# SOLUTION #############\n");
  201.         possible_points.clear(); /*Reset */
  202.         array_col.clear();
  203.         total_height = 0;
  204.         total_width = 0;
  205.         for (int r = 0; r < n_rectangles; r++) {
  206.             array_rec[r].placed = false;
  207.         }
  208.         for (int j = 0; j < n_rectangles; j++) {
  209.             if (!array_rec[j].first_placed_vertical) {
  210.                 printf("FIRST PLACE %d \n", j);
  211.                 array_rec[j].first_placed_vertical = true;
  212.                 printf("chamou func 0\n");
  213.                 Point origin = {0, 0};
  214.                 possible_points.push_back(origin);
  215.                 return solution(array_rec, array_col, solutions, area, possible_points, removed_points, j);
  216.             }
  217.         }
  218.         printf("RETURN FALSE SAIDA %d\n", array_rec[2].first_placed_vertical);
  219.  
  220.         return false;
  221.  
  222.     }
  223.  
  224.     if (rec == n_rectangles) {
  225.         rec = 0;
  226.         printf("chamou func 2\n");
  227.         return solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec);
  228.     }
  229.  
  230.     if (array_rec[rec].placed) {
  231.         printf("chamou func 3\n");
  232.         return solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec + 1);
  233.     }
  234.  
  235.     if (possible_points.empty()) {
  236.         for (int j = 0; j < n_rectangles; j++) {
  237.             if (array_rec[j].placed == true) {
  238.                 printf("ADEUS\n");
  239.                 return false;
  240.             }
  241.         }
  242.         printf("FIRST PLACED %d\n", rec);
  243.         Point point = {0, 0};
  244.         place_rectangle(array_rec, possible_points, point, rec);
  245.         array_rec[rec].placed = true;
  246.         array_rec[rec].first_placed_vertical = true;
  247.  
  248.         array_col.push_back(array_rec[rec]);
  249.         printf("chamou func 4\n");
  250.         return solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec + 1);
  251.     } else {
  252.         int processed_points = 0;
  253.         for (int i = 0; i < (int) possible_points.size(); i++) {
  254.             if (place_rectangle(array_rec, possible_points, possible_points[i], rec)) {
  255.                 removed_points.push_back(possible_points[i]);
  256.                 possible_points.erase(possible_points.begin() + i);
  257.  
  258.                 array_rec[rec].placed = true;
  259.                 array_col.push_back(array_rec[rec]);
  260.  
  261.  
  262.                 printf("chamou func 5\n");
  263.                 return solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec + 1);
  264.             } else {
  265.                 processed_points++;
  266.                 if (all_rectangle_tested(possible_points[i], array_col)) {
  267.                     removed_points.push_back(possible_points[i]);
  268.                     possible_points.erase(possible_points.begin() + i);
  269.                     processed_points--;
  270.                     printf("APAGAMOS PONTO");
  271.                     for (int a = 0; a < (int) possible_points.size(); a++) {
  272.                         possible_points[a].visited = 0;
  273.                     }
  274.                     array_rec[rec].placed = false;
  275.                     printf("chamou func 6\n");
  276.                     //solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec);
  277.                 }
  278.             }
  279.         }
  280.         if (processed_points == (int) possible_points.size()) {
  281.  
  282.  
  283.             for (int l = 0; l < (int) removed_points.size(); l++) {
  284.                 if (array_rec[rec].blc[0] == removed_points[l].x && array_rec[rec].blc[1] == removed_points[l].y && array_rec[rec].placed) {
  285.                     /*if (!all_rectangle_tested(removed_points[l], array_col)) {
  286.                         printf("TODOS TESTADOS NO PONTO %d %d\n", removed_points[l].x, removed_points[l].y);*/
  287.                         possible_points.push_back(removed_points[l]);
  288.  
  289.  
  290.                 }
  291.             }
  292.             for (int k = 0; k < n_rectangles; k++) {
  293.                 if (array_rec[k].blc[0] == array_col.back().blc[0] && array_rec[k].blc[1] == array_col.back().blc[1]) {
  294.                     printf("ENTROU jhbskjv\n");
  295.                     array_rec[k].placed = false;
  296.                 }
  297.             }
  298.             array_col.pop_back();
  299.             printf("chamou func 7\n");
  300.             return solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec+1);
  301.  
  302.         }
  303.         solution(array_rec, array_col, solutions, area, possible_points, removed_points, rec);
  304.  
  305.     }
  306.  
  307.     return false;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement