Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef struct rectangle {
  10.     int width,height;
  11.      int urc [2],ulc[2],brc[2],blc[2];
  12.     bool placed;
  13.  
  14. } Rectangle;
  15.  
  16. typedef struct point {
  17.     int x,y;
  18. }Point;
  19.  
  20. int area,total_height,total_width,n_rectangles;
  21. int solutions=0;
  22.  
  23.  
  24.  
  25. bool check_all_placed(Rectangle *array_rec){
  26.     int placed=0;
  27.     for(int i=0;i<n_rectangles;i++){
  28.         if(array_rec[i].placed){
  29.             placed++;
  30.         }
  31.     }
  32.     printf("NUM PLACED %d\n",placed);
  33.     if(placed==n_rectangles){
  34.         return true;
  35.     }
  36.     else{
  37.         return false;
  38.     }
  39. }
  40.  
  41. void vertical_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  42.     array_rec[sq].blc[0] = blcx;
  43.     array_rec[sq].blc[1] = blcy;
  44.     array_rec[sq].brc[0] = blcx+array_rec[sq].width;
  45.     array_rec[sq].brc[1] = blcy;
  46.     array_rec[sq].ulc[0] = blcx;
  47.     array_rec[sq].ulc[1] = blcy+array_rec[sq].height;
  48.     array_rec[sq].urc[0] = blcx+array_rec[sq].width;
  49.     array_rec[sq].urc[1] = blcy+array_rec[sq].height;
  50. }
  51.  
  52. void horizontal_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  53.     array_rec[sq].blc[0] = blcx;
  54.     array_rec[sq].blc[1] = blcy;
  55.     array_rec[sq].brc[0] = blcx+array_rec[sq].height;
  56.     array_rec[sq].brc[1] = blcy;
  57.     array_rec[sq].ulc[0] = blcx;
  58.     array_rec[sq].ulc[1] = blcy+array_rec[sq].width;
  59.     array_rec[sq].urc[0] = blcx+array_rec[sq].height;
  60.     array_rec[sq].urc[1] = blcy+array_rec[sq].width;
  61. }
  62. bool check_intersection(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec){ /*Está a dar merda*/
  63.     if (toplace.x == 0 && toplace.y==0) {
  64.         total_width = array_rec[rec].width;
  65.         total_height = array_rec[rec].height;
  66.         return true;
  67.     }
  68.     if(toplace.y==total_height){ /*em cima */
  69.         if(toplace.x+array_rec[rec].width>total_width){
  70.             printf("TO y %d wid %d  total%d\n",toplace.x,array_rec[rec].width,total_width);
  71.  
  72.             printf("FALSE 1\n");
  73.             return false;
  74.         }
  75.     }
  76.     else{ /*de lado*/
  77.         if(toplace.x == total_width) {
  78.             if (toplace.y + array_rec[rec].height > total_height) {
  79.                 printf("TO y %d height%d ",toplace.y,array_rec[rec].height);
  80.                 printf("FALSE 2\n");
  81.                 return false;
  82.             }
  83.         }
  84.     }
  85.  
  86.     long auxtotal_width=total_width;
  87.     long auxtotal_height=total_height;
  88.     if(total_height<array_rec[rec].ulc[1]){
  89.         auxtotal_height=array_rec[rec].ulc[1];
  90.     }
  91.  
  92.     if(total_width<array_rec[rec].brc[0]) {
  93.         auxtotal_width =array_rec[rec].brc[0];
  94.     }
  95.     printf("Largura Total: %ld Altura Total: %ld\n",auxtotal_width,auxtotal_height);
  96.     if((auxtotal_width)*auxtotal_height<=area){
  97.         total_width=auxtotal_width;
  98.         total_height=auxtotal_height;
  99.         return true;
  100.     }
  101.     else{
  102.  
  103.         return false;
  104.     }
  105.  
  106. }
  107.  
  108. bool place_rectangle_vertical(Rectangle *array_rec,vector<Point> &possible_points,Point toplace, int rec){
  109.  
  110.     vertical_coordinates(array_rec, rec, toplace.x, toplace.y);
  111.     printf("To Place: %d %d\n", toplace.x, toplace.y);
  112.     printf("Total width %d | Total height %d\n",total_width,total_height);
  113.     if(check_intersection(array_rec,possible_points,toplace,rec)) {
  114.         Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
  115.         Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
  116.  
  117.         for(int s=0;s<(int)possible_points.size();s++){
  118.                 if(possible_points[s].x==toplace.x && possible_points[s].y==toplace.y){
  119.                     possible_points.erase(possible_points.begin()+s);
  120.                 }
  121.             }
  122.         possible_points.push_back(ulc);
  123.         possible_points.push_back(brc);
  124.         array_rec[rec].placed=true;
  125.        
  126.         return true;
  127.     }
  128.     return false;
  129. }
  130.  
  131. bool place_rectangle_horizontal(Rectangle *array_rec,vector<Point> &possible_points,Point toplace, int rec){
  132.  
  133.         printf("rodou\n");
  134.         printf("To Place Horiz: %d %d\n", toplace.x, toplace.y);
  135.         horizontal_coordinates(array_rec,rec,toplace.x,toplace.y);
  136.         if(check_intersection(array_rec,possible_points,toplace,rec)){
  137.  
  138.             Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
  139.             Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
  140.               for(int s=0;s<(int)possible_points.size();s++){
  141.                 if(possible_points[s].x==toplace.x && possible_points[s].y==toplace.y){
  142.                     possible_points.erase(possible_points.begin()+s);
  143.                 }
  144.             }
  145.             possible_points.push_back(ulc);
  146.             possible_points.push_back(brc);
  147.             array_rec[rec].placed=true;
  148.          
  149.             return true;
  150.         }
  151.         return false;
  152. }
  153.  
  154.  
  155.  
  156. bool solution(Rectangle* array_rec,vector<Point> &possible_points){
  157.     for(int j=0;j<(int)possible_points.size();j++){
  158.         cout<<"possible_points: ";
  159.         cout<<possible_points[j].x<<"  "<<possible_points[j].y<<"\n";
  160.     }
  161.  
  162.     if (check_all_placed(array_rec)){
  163.         Point p={0,0};
  164.      
  165.         solutions++;
  166.         printf("##########SOLUTIONS###################");
  167.         for(int i=0;i<n_rectangles;i++){
  168.             array_rec[i].placed=false;
  169.         }
  170.         possible_points.clear();
  171.         possible_points.push_back(p);
  172.  
  173.         return true;
  174.     }
  175.     else{
  176.             for(int i =0;i<n_rectangles;i++){
  177.                 for(int j=0;j<(int)possible_points.size();j++){
  178.                    Rectangle* array_rec_copy=new Rectangle[n_rectangles];
  179.                     //vector<Point> possible_points_copy=possible_points;
  180.  
  181.                     for(int k=0;k<n_rectangles;k++){
  182.                         array_rec_copy[k].placed=array_rec[k].placed;
  183.                          array_rec_copy[k].blc[0]=array_rec[k].blc[0];
  184.                           array_rec_copy[k].blc[1]=array_rec[k].blc[1];
  185.                           array_rec_copy[k].brc[0]=array_rec[k].brc[0];
  186.                           array_rec_copy[k].brc[1]=array_rec[k].brc[1];
  187.                            array_rec_copy[k].height=array_rec[k].height;
  188.                             array_rec_copy[k].width=array_rec[k].width;
  189.                              array_rec_copy[k].ulc[0]=array_rec[k].ulc[0];
  190.                             array_rec_copy[k].ulc[1]=array_rec[k].ulc[1];
  191.                             array_rec_copy[k].urc[0]=array_rec[k].urc[0];
  192.                             array_rec_copy[k].urc[1]=array_rec[k].urc[1];
  193.                            
  194.                     }
  195.                     if(place_rectangle_vertical(array_rec_copy,possible_points,possible_points[j],i)){
  196.                         array_rec_copy[i].placed=true;
  197.                         solution(array_rec_copy,possible_points);
  198.                     }
  199.                     if(array_rec[i].width!=array_rec[i].height){
  200.                         if(place_rectangle_horizontal(array_rec_copy,possible_points,possible_points[j],i)){
  201.                             array_rec_copy[i].placed=true;
  202.                             solution(array_rec_copy,possible_points);
  203.                         }
  204.                     }
  205.                 }
  206.             }
  207.         }
  208.    
  209.        
  210.        
  211.    
  212.     return false;
  213. }
  214.  
  215.  
  216.  
  217. int main() {
  218.     Rectangle* array_rec=new Rectangle[n_rectangles]; /* array que guarda as medidas de cada rectangulo*/
  219.     vector <Point> possible_points;
  220.     scanf("%d",&n_rectangles);
  221.     for(int i=0; i<n_rectangles; i++) {
  222.         fscanf(stdin,"%d %d",&array_rec[i].width,&array_rec[i].height);
  223.         array_rec[i].placed=false;
  224.         area += (array_rec[i].width * array_rec[i].height);
  225.     }
  226.     printf("Área Total:%d\n",area);
  227.     Point p={0,0};
  228.     possible_points.push_back(p);
  229.     solution(array_rec,possible_points);
  230.     printf("%d",solutions);
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement