Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <string>
- #include <algorithm>
- using namespace std;
- typedef struct rectangle {
- int width,height;
- 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;
- 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 check_intersection(Rectangle *array_rec,vector<Point> &possible_points,Point toplace,int rec){ /*Está a dar merda*/
- if (toplace.x == 0 && toplace.y==0) {
- total_width = array_rec[rec].width;
- total_height = array_rec[rec].height;
- return true;
- }
- if(toplace.y==total_height){ /*em cima */
- if(toplace.x+array_rec[rec].width>total_width){
- printf("TO y %d wid %d total%d\n",toplace.x,array_rec[rec].width,total_width);
- printf("FALSE 1\n");
- return false;
- }
- }
- else{ /*de lado*/
- if(toplace.x == total_width) {
- if (toplace.y + array_rec[rec].height > total_height) {
- printf("TO y %d height%d ",toplace.y,array_rec[rec].height);
- printf("FALSE 2\n");
- return false;
- }
- }
- }
- long auxtotal_width=total_width;
- long auxtotal_height=total_height;
- if(total_height<array_rec[rec].ulc[1]){
- auxtotal_height=array_rec[rec].ulc[1];
- }
- if(total_width<array_rec[rec].brc[0]) {
- auxtotal_width =array_rec[rec].brc[0];
- }
- printf("Largura Total: %ld Altura Total: %ld\n",auxtotal_width,auxtotal_height);
- if((auxtotal_width)*auxtotal_height<=area){
- total_width=auxtotal_width;
- total_height=auxtotal_height;
- return true;
- }
- else{
- return false;
- }
- }
- bool place_rectangle_vertical(Rectangle *array_rec,vector<Point> &possible_points,Point toplace, int rec){
- vertical_coordinates(array_rec, rec, toplace.x, toplace.y);
- printf("To Place: %d %d\n", toplace.x, toplace.y);
- printf("Total width %d | Total height %d\n",total_width,total_height);
- if(check_intersection(array_rec,possible_points,toplace,rec)) {
- Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
- Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
- for(int s=0;s<(int)possible_points.size();s++){
- if(possible_points[s].x==toplace.x && possible_points[s].y==toplace.y){
- possible_points.erase(possible_points.begin()+s);
- }
- }
- possible_points.push_back(ulc);
- possible_points.push_back(brc);
- array_rec[rec].placed=true;
- return true;
- }
- return false;
- }
- bool place_rectangle_horizontal(Rectangle *array_rec,vector<Point> &possible_points,Point toplace, int rec){
- printf("rodou\n");
- printf("To Place Horiz: %d %d\n", toplace.x, toplace.y);
- horizontal_coordinates(array_rec,rec,toplace.x,toplace.y);
- if(check_intersection(array_rec,possible_points,toplace,rec)){
- Point ulc = {array_rec[rec].ulc[0], array_rec[rec].ulc[1]};
- Point brc = {array_rec[rec].brc[0], array_rec[rec].brc[1]};
- for(int s=0;s<(int)possible_points.size();s++){
- if(possible_points[s].x==toplace.x && possible_points[s].y==toplace.y){
- possible_points.erase(possible_points.begin()+s);
- }
- }
- possible_points.push_back(ulc);
- possible_points.push_back(brc);
- array_rec[rec].placed=true;
- return true;
- }
- return false;
- }
- bool solution(Rectangle* array_rec,vector<Point> &possible_points){
- for(int j=0;j<(int)possible_points.size();j++){
- cout<<"possible_points: ";
- cout<<possible_points[j].x<<" "<<possible_points[j].y<<"\n";
- }
- if (check_all_placed(array_rec)){
- Point p={0,0};
- solutions++;
- printf("##########SOLUTIONS###################");
- for(int i=0;i<n_rectangles;i++){
- array_rec[i].placed=false;
- }
- possible_points.clear();
- possible_points.push_back(p);
- return true;
- }
- else{
- for(int i =0;i<n_rectangles;i++){
- for(int j=0;j<(int)possible_points.size();j++){
- Rectangle* array_rec_copy=new Rectangle[n_rectangles];
- //vector<Point> possible_points_copy=possible_points;
- for(int k=0;k<n_rectangles;k++){
- array_rec_copy[k].placed=array_rec[k].placed;
- array_rec_copy[k].blc[0]=array_rec[k].blc[0];
- array_rec_copy[k].blc[1]=array_rec[k].blc[1];
- array_rec_copy[k].brc[0]=array_rec[k].brc[0];
- array_rec_copy[k].brc[1]=array_rec[k].brc[1];
- array_rec_copy[k].height=array_rec[k].height;
- array_rec_copy[k].width=array_rec[k].width;
- array_rec_copy[k].ulc[0]=array_rec[k].ulc[0];
- array_rec_copy[k].ulc[1]=array_rec[k].ulc[1];
- array_rec_copy[k].urc[0]=array_rec[k].urc[0];
- array_rec_copy[k].urc[1]=array_rec[k].urc[1];
- }
- if(place_rectangle_vertical(array_rec_copy,possible_points,possible_points[j],i)){
- array_rec_copy[i].placed=true;
- solution(array_rec_copy,possible_points);
- }
- if(array_rec[i].width!=array_rec[i].height){
- if(place_rectangle_horizontal(array_rec_copy,possible_points,possible_points[j],i)){
- array_rec_copy[i].placed=true;
- solution(array_rec_copy,possible_points);
- }
- }
- }
- }
- }
- return false;
- }
- int main() {
- Rectangle* array_rec=new Rectangle[n_rectangles]; /* array que guarda as medidas de cada rectangulo*/
- vector <Point> possible_points;
- scanf("%d",&n_rectangles);
- for(int i=0; i<n_rectangles; i++) {
- fscanf(stdin,"%d %d",&array_rec[i].width,&array_rec[i].height);
- array_rec[i].placed=false;
- area += (array_rec[i].width * array_rec[i].height);
- }
- printf("Área Total:%d\n",area);
- Point p={0,0};
- possible_points.push_back(p);
- solution(array_rec,possible_points);
- printf("%d",solutions);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement