Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- typedef struct rectangle {
- int width,height;/*width->largura | height->comprimento*/
- int urc [2],ulc[2],brc[2],blc[2];
- bool placed;
- } Rectangle;
- typedef struct point {
- int x,y;
- }Point;
- int area,total_height,total_width,n_rectangles;
- int solutions=0;
- pair <Point,Point> array_sol[100000][8];
- bool check_all_placed(Rectangle *array_rec){
- int placed=0;
- for(int i=0;i<n_rectangles;i++){
- if(array_rec[i].placed){
- placed++;
- }
- }
- //printf("NUM PLACED %d\n",placed);
- if(placed==n_rectangles){
- return true;
- }
- else{
- return false;
- }
- }
- void vertical_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
- array_rec[sq].blc[0] = blcx;
- array_rec[sq].blc[1] = blcy;
- array_rec[sq].brc[0] = blcx+array_rec[sq].width;
- array_rec[sq].brc[1] = blcy;
- array_rec[sq].ulc[0] = blcx;
- array_rec[sq].ulc[1] = blcy+array_rec[sq].height;
- array_rec[sq].urc[0] = blcx+array_rec[sq].width;
- array_rec[sq].urc[1] = blcy+array_rec[sq].height;
- }
- void horizontal_coordinates(Rectangle *array_rec,int sq,int blcx,int blcy) {
- array_rec[sq].blc[0] = blcx;
- array_rec[sq].blc[1] = blcy;
- array_rec[sq].brc[0] = blcx+array_rec[sq].height;
- array_rec[sq].brc[1] = blcy;
- array_rec[sq].ulc[0] = blcx;
- array_rec[sq].ulc[1] = blcy+array_rec[sq].width;
- array_rec[sq].urc[0] = blcx+array_rec[sq].height;
- array_rec[sq].urc[1] = blcy+array_rec[sq].width;
- }
- bool place_rectangle(Rectangle *array_rec,vector<Point> &possible_points,int pos, int rec,int orientation){//pos=posição do ponto no possible point || 0->vertical 1->horizontal
- Point toplace;
- toplace.x=possible_points[pos].x;
- toplace.y=possible_points[pos].y;
- Point ulc;
- Point brc;
- if (orientation==0){
- vertical_coordinates(array_rec, rec, toplace.x, toplace.y);
- }
- else{
- horizontal_coordinates(array_rec, rec, toplace.x, toplace.y);
- }
- if (toplace.x == 0 && toplace.y==0) {
- possible_points.pop_back();//tirar (0,0)
- total_width = array_rec[rec].width;
- total_height = array_rec[rec].height;
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- brc.x=array_rec[rec].brc[0];
- brc.y=array_rec[rec].brc[1];
- possible_points.push_back(ulc);
- possible_points.push_back(brc);
- array_rec[rec].placed=true;
- return true;
- }
- else if (toplace.x==0){
- // printf("array rec %d possible %d",array_rec[rec].brc[0],possible_points[pos+1].x);
- if(array_rec[rec].brc[0]<possible_points[pos+1].x){
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- brc.x=array_rec[rec].brc[0];
- brc.y=array_rec[rec].brc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,brc);
- possible_points.insert(possible_points.begin()+pos,ulc);
- total_height+=array_rec[rec].height;
- return true;
- }
- if(array_rec[rec].brc[0]==possible_points[pos+1].x){
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,ulc);
- total_height+=array_rec[rec].height;
- return true;
- }
- return false;
- }
- else if (toplace.y==0){
- if(array_rec[rec].ulc[1]<possible_points[pos-1].y){
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- brc.x=array_rec[rec].brc[0];
- brc.y=array_rec[rec].brc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,brc);
- possible_points.insert(possible_points.begin()+pos,ulc);
- total_width+=array_rec[rec].width;
- return true;
- }
- if(array_rec[rec].ulc[1]==possible_points[pos-1].y){
- brc.x=array_rec[rec].brc[0];
- brc.y=array_rec[rec].brc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,brc);
- total_width+=array_rec[rec].width;
- return true;
- }
- return false;
- }
- if (toplace.x!=0 && toplace.y!=0){
- if(array_rec[rec].ulc[1]<possible_points[pos-1].y && array_rec[rec].brc[0]<possible_points[pos+1].x){
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- possible_points.insert(possible_points.begin()+pos,brc);
- possible_points.insert(possible_points.begin()+pos,ulc);
- return true;
- }
- if(array_rec[rec].ulc[1]<possible_points[pos-1].y && array_rec[rec].brc[0]==possible_points[pos+1].x){
- ulc.x=array_rec[rec].ulc[0];
- ulc.y=array_rec[rec].ulc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,ulc);
- return true;
- }
- if(array_rec[rec].ulc[1]==possible_points[pos-1].y && array_rec[rec].brc[0]<possible_points[pos+1].x){
- brc.x=array_rec[rec].brc[0];
- brc.y=array_rec[rec].brc[1];
- possible_points.erase(possible_points.begin()+pos);
- possible_points.insert(possible_points.begin()+pos,brc);
- return true;
- }
- if(array_rec[rec].ulc[1]==possible_points[pos-1].y && array_rec[rec].brc[0]==possible_points[pos+1].x){
- possible_points.erase(possible_points.begin()+pos);
- return true;
- }
- }
- return false;
- }
- bool compare_Points(const pair <Point,Point> &p1,const pair <Point,Point> &p2){
- if(p1.first.x<p2.first.x){
- return true;
- }
- if(p1.first.x>p2.first.x){
- return false;
- }
- if(p1.second.x<p2.second.x){
- return true;
- }
- if(p1.second.x>p2.second.x){
- return false;
- }
- if(p1.first.y<p2.first.y){
- return true;
- }
- if(p1.first.y>p2.first.y){
- return false;
- }
- if(p1.second.y<p2.second.y){
- return true;
- }
- if(p1.second.y>p2.second.y){
- return false;
- }
- return false;
- }
- bool solution(Rectangle* array_rec,vector<Point> &possible_points){
- if (check_all_placed(array_rec)){
- Point ulc,brc;
- pair <Point,Point> rec_aux;
- if((int)possible_points.size()==2){
- vector<pair <Point,Point> > array_sol_aux;
- for(int i=0;i<n_rectangles;i++){
- ulc.x=array_rec[i].ulc[0];
- ulc.y=array_rec[i].ulc[1];
- brc.x=array_rec[i].brc[0];
- brc.y=array_rec[i].brc[1];
- rec_aux=make_pair(ulc,brc);
- array_sol_aux.push_back(rec_aux);
- }
- sort(array_sol_aux.begin(),array_sol_aux.end(),compare_Points);
- /*for(int i=0;i<(int)array_sol_aux.size();i++){
- printf("(%d %d) (%d,%d)\n",array_sol_aux[i].first.x,array_sol_aux[i].first.y,array_sol_aux[i].second.x,array_sol_aux[i].second.y);
- }*/
- if (solutions == 0) {
- for (int j = 0; j < n_rectangles; j++) {
- //cout << "VAZIO\n";
- array_sol[0][j].first.x = array_sol_aux[j].first.x;
- array_sol[0][j].first.y = array_sol_aux[j].first.y;
- array_sol[0][j].second.x = array_sol_aux[j].second.x;
- array_sol[0][j].second.y = array_sol_aux[j].second.y;
- }
- solutions++;
- //printf("##########SOLUTIONS###################\n");
- return true;
- }else{
- for (int i = 0; i < solutions; i++) {
- int igual = 0;
- for (int j = 0; j < n_rectangles; j++) {
- //igual=0;
- if (array_sol[i][j].first.x == array_sol_aux[j].first.x &&
- array_sol[i][j].first.y == array_sol_aux[j].first.y &&
- array_sol[i][j].second.x == array_sol_aux[j].second.x &&
- array_sol[i][j].second.y == array_sol_aux[j].second.y) {
- igual++;
- }
- }
- if (igual == n_rectangles) {
- //cout << "igual\n";
- return false;
- }
- }
- for (int j = 0; j < n_rectangles; j++) {
- array_sol[solutions][j].first.x = array_sol_aux[j].first.x;
- array_sol[solutions][j].first.y = array_sol_aux[j].first.y;
- array_sol[solutions][j].second.x = array_sol_aux[j].second.x;
- array_sol[solutions][j].second.y = array_sol_aux[j].second.y;
- }
- solutions++;
- return true;
- }
- }
- return false;
- }
- else{
- for(int i =0;i<n_rectangles;i++){
- for(int j=0;j<(int)possible_points.size();j++){
- vector<Point> possible_points_copy=possible_points;
- vector<Point> possible_points_copy_horizontal=possible_points;
- if(!array_rec[i].placed){
- if(place_rectangle(array_rec,possible_points_copy,j,i,0)){
- //printf("dentro\n");
- array_rec[i].placed=true;
- solution(array_rec,possible_points_copy);
- }
- if(array_rec[i].width!=array_rec[i].height){
- if(place_rectangle(array_rec,possible_points_copy_horizontal,j,i,1)){
- // printf("dentro hori\n");
- array_rec[i].placed=true;
- solution(array_rec,possible_points_copy_horizontal);
- }
- }
- array_rec[i].placed=false;
- }
- }
- }
- }
- return false;
- }
- int main() {
- Rectangle* array_rec=new Rectangle[n_rectangles]; /* array que guarda as medidas de cada rectangulo*/
- vector <Point> possible_points;
- vector<pair <Point,Point> > array_sol;
- int width,height;
- scanf("%d",&n_rectangles);
- for(int i=0; i< n_rectangles; i++) {
- scanf("%d %d",&width,&height);
- array_rec[i].width=width;
- array_rec[i].height=height;
- array_rec[i].placed=false;
- area += (array_rec[i].width * array_rec[i].height);
- }
- Point p={0,0};
- possible_points.push_back(p);
- solution(array_rec,possible_points);
- printf("%d\n",solutions);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement