Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.46 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;
  18.  
  19. }Point;
  20.  
  21. bool check_intersection(Rectangle *array_rec,vector<Point> &occupied_points,Point toplace,int rec,int orientation);
  22. bool place_rectangle(Rectangle *array_rec,vector<Point> &occupied_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,int*solutions, long* area,vector<Point> &occupied_points,int rec);
  27.  
  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 <Point> occupied_points;
  33.     scanf("%d",&n_rectangles);
  34.     for(int i=0;i<n_rectangles;i++){
  35.         fscanf(stdin,"%d %d",&array_rec[i].width,&array_rec[i].height);
  36.         array_rec[i].placed=false;
  37.         array_rec[i].first_placed_vertical=false;
  38.         array_rec[i].first_placed_horizontal=false;
  39.         area += (array_rec[i].width * array_rec[i].height);
  40.     }
  41.     printf("Área Total:%ld\n",area);
  42.     solution(array_rec,&solutions,&area,occupied_points,0);
  43.     printf("%d",solutions);
  44.     return 0;
  45. }
  46.  
  47. void vertical_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  48.     array_rec[sq].blc[0] = blcx;
  49.     array_rec[sq].blc[1] = blcy;
  50.     array_rec[sq].brc[0] = blcx+array_rec[sq].width;
  51.     array_rec[sq].brc[1] = blcy;
  52.     array_rec[sq].ulc[0] = blcx;
  53.     array_rec[sq].ulc[1] = blcy+array_rec[sq].height;
  54.     array_rec[sq].urc[0] = blcx+array_rec[sq].width;
  55.     array_rec[sq].urc[1] = blcy+array_rec[sq].height;
  56. }
  57.  
  58. void horizontal_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
  59.     array_rec[sq].blc[0] = blcx;
  60.     array_rec[sq].blc[1] = blcy;
  61.     array_rec[sq].brc[0] = blcx+array_rec[sq].height;
  62.     array_rec[sq].brc[1] = blcy;
  63.     array_rec[sq].ulc[0] = blcx;
  64.     array_rec[sq].ulc[1] = blcy+array_rec[sq].width;
  65.     array_rec[sq].urc[0] = blcx+array_rec[sq].height;
  66.     array_rec[sq].urc[1] = blcy+array_rec[sq].width;
  67. }
  68.  
  69. bool accept_solution(Rectangle* array_rec){
  70.  
  71.     for(int i=0;i<n_rectangles;i++){
  72.         if(!array_rec[i].placed){
  73.             return false;
  74.         }
  75.     }
  76.     return true;
  77. }
  78.  
  79.  
  80. bool solution(Rectangle *array_rec,int*solutions, long* area,vector<Point> &occupied_points,int rec){
  81.     int first_placed=0;
  82.     for(int j=0;j<n_rectangles;j++){
  83.         if(array_rec[j].first_placed_vertical){
  84.             first_placed++;
  85.         }
  86.     }
  87.     if(first_placed==n_rectangles){
  88.         return true;
  89.     }
  90.  
  91.     if(accept_solution(array_rec)){
  92.         for(int i=0;i<n_rectangles;i++){
  93.             array_rec[i].placed=false;
  94.         }
  95.         total_height=0;
  96.         total_width=0;
  97.         occupied_points.clear();
  98.         return true;
  99.     }
  100.  
  101.     for(int i=0;i<(n_rectangles);i++){
  102.         if(!array_rec[i].placed){
  103.             if(occupied_points.empty()){
  104.                 if(!array_rec[i].first_placed_vertical) {
  105.                     Point point = {0, 0};
  106.                     place_rectangle(array_rec, occupied_points, point, i);
  107.                     array_rec[i].placed = true;
  108.                     array_rec[i].first_placed_vertical = true;
  109.                     solution(array_rec, solutions, area, occupied_points, i + 1);
  110.                 }
  111.             }
  112.             else{
  113.                 for(int p=0;p<(int)occupied_points.size();p++){
  114.                     if(place_rectangle(array_rec,occupied_points,occupied_points[p],i)){
  115.                         array_rec[i].placed=true;
  116.                         if(solution(array_rec,solutions,area,occupied_points,i+1)){
  117.                             *solutions+=1;
  118.                             total_height=0;
  119.                             total_width=0;
  120.                             occupied_points.clear();
  121.                         }
  122.                         array_rec[i].placed=false;
  123.                     }
  124.                 }if(array_rec[i].placed==false){
  125.                     Point aux;
  126.                     for (int x = 0; x<n_rectangles;x++){
  127.                         if(array_rec[x].placed){
  128.                             if( array_rec[x].ulc[1]==occupied_points[(int)occupied_points.size()-2].y &&  array_rec[x].ulc[0]==occupied_points[(int)occupied_points.size()-2].x && array_rec[x].brc[1]==occupied_points[(int)occupied_points.size()-1].y &&  array_rec[x].brc[0]==occupied_points[(int)occupied_points.size()-1].x  ){
  129.                                 array_rec[x].ulc[0]=NULL;
  130.                                 array_rec[x].ulc[1]=NULL;
  131.                                 array_rec[x].brc[0]=NULL;
  132.                                 array_rec[x].brc[1]=NULL;
  133.                                 array_rec[x].placed=false;
  134.                                 aux={array_rec[x].blc[0],array_rec[x].blc[1]};
  135.  
  136.  
  137.                             }
  138.  
  139.  
  140.                         }
  141.                     }
  142.                     occupied_points.erase(occupied_points.end()-1);
  143.                     occupied_points.erase(occupied_points.end()-2);
  144.                     occupied_points.push_back(aux);
  145.  
  146.  
  147.  
  148.                 }
  149.             }
  150.  
  151.         }
  152.     }
  153.     return false;
  154. }
  155.  
  156. bool place_rectangle(Rectangle *array_rec,vector<Point> &occupied_points,Point toplace,int rec){
  157.     printf("Rectangulo: %d\n",rec);
  158.     Rectangle aux_rec=array_rec[rec];
  159.     vertical_coordinates(array_rec, rec, toplace.x, toplace.y);
  160.     printf("To Place: %d %d\n", toplace.x, toplace.y);
  161.     if(check_intersection(array_rec,occupied_points,toplace,rec,0)) {
  162.         Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
  163.         Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
  164.         printf("Point ulc: %d %d Point brc: %d %d \n", ulc.x, ulc.y, brc.x, brc.y);
  165.         occupied_points.push_back(ulc);
  166.         occupied_points.push_back(brc);
  167.       for(int s=0;s<(int)occupied_points.size();s++){
  168.           if(occupied_points[s].x==toplace.x && occupied_points[s].y==toplace.y){
  169.               occupied_points.erase(occupied_points.begin()+s);
  170.           }
  171.       }
  172.         return true;
  173.     }
  174.     else {
  175.         printf("rodou\n");
  176.         printf("To Place Horiz: %d %d\n", toplace.x, toplace.y);
  177.         horizontal_coordinates(array_rec,rec,toplace.x,toplace.y);
  178.         if(check_intersection(array_rec,occupied_points,toplace,rec,1)){
  179.  
  180.             Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
  181.             Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
  182.             printf("Point ulc: %d %d Point brc: %d %d \n", ulc.x, ulc.y, brc.x, brc.y);
  183.             occupied_points.push_back(ulc);
  184.             occupied_points.push_back(brc);
  185.             for(int s=0;s<(int)occupied_points.size();s++){
  186.                 if(occupied_points[s].x==toplace.x && occupied_points[s].y==toplace.y){
  187.                     occupied_points.erase(occupied_points.begin()+s);
  188.                 }
  189.             }
  190.             return true;
  191.         }
  192.         array_rec[rec]=aux_rec;
  193.         return false;
  194.     }
  195. }
  196.  
  197. bool check_intersection(Rectangle *array_rec,vector<Point> &occupied_points,Point toplace,int rec,int orientation){ /* orientation: 0 vertical 1 horizontal*/
  198.     for (int i = 0; i < (int) occupied_points.size(); i++) {
  199.         if(orientation==0) {
  200.             if (toplace.x + array_rec[rec].width < occupied_points[i].x && toplace.y + array_rec[rec].height < occupied_points[i].y) {
  201.                 printf("false intercestion\n");
  202.                 return false;
  203.             }
  204.         }
  205.         else{
  206.             if (toplace.x + array_rec[rec].height < occupied_points[i].x && toplace.y + array_rec[rec].width < occupied_points[i].y) {
  207.                 printf("false intercestion\n");
  208.                 return false;
  209.             }
  210.         }
  211.  
  212.     }
  213.     long auxtotal_width=total_width;
  214.     long auxtotal_height=total_height;
  215.     if(total_height<array_rec[rec].ulc[1]){
  216.         auxtotal_height=array_rec[rec].ulc[1];
  217.     }
  218.  
  219.     if(total_width<array_rec[rec].brc[0]) {
  220.         auxtotal_width =array_rec[rec].brc[0];
  221.     }
  222.     printf("w %ld h %ld\n",auxtotal_width,auxtotal_height);
  223.     if((auxtotal_width)*auxtotal_height<=area){
  224.         total_width=auxtotal_width;
  225.         total_height=auxtotal_height;
  226.         return true;
  227.     }
  228.     else{
  229.         printf("false area \n");
  230.  
  231.         return false;
  232.     }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement