Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <sstream>
- #include <string>
- using namespace std;
- class Ponto {
- public:
- int x;
- int y;
- int active;
- int sup;
- };
- class Retangulo {
- public:
- int x;
- int y;
- int id;
- Ponto sup;
- Ponto inf;
- };
- int nr_rets;
- int nr_configs = 0;
- int nr_rec = 0;
- int area = 0;
- bool check_intersections(Retangulo r, vector<Retangulo> colocados){
- if (colocados.size() == 0 || colocados.size() == 1){
- return false;
- }
- //Percorrer retangulos ja colocados
- for (int i=0; i<colocados.size(); i++){
- if (r.sup.x < colocados.at(i).inf.x && r.inf.x > colocados.at(i).sup.x && r.sup.y > colocados.at(i).inf.y && r.inf.y < colocados.at(i).sup.y){
- //Existe interseção
- cout << "Intersetei";
- return true;
- }
- }
- return false;
- }
- bool check_retangle(vector<Retangulo> colocados, Retangulo r){
- for (int i=0; i<colocados.size(); i++){
- if (r.id == colocados.at(i).id){
- //cout << "oi";
- return true;
- }
- }
- return false;
- }
- void mostra_pts(vector<Ponto> pts){
- int i;
- for(i=0;i<pts.size();i++){
- cout <<"X:" << pts.at(i).x << " Y:" << pts.at(i).y << " Active: "<< pts.at(i).active <<" \n";
- }
- cout << "\n";
- }
- bool fitting_retangle(int x, int y){
- int area_to_fill = 0;
- area_to_fill = x * y;
- if(area < area_to_fill){
- return true;
- }
- return false;
- }
- bool is_retangle(vector<Ponto> pts){
- int area_ret;
- int x,y;
- vector<Ponto> active;
- Ponto p;
- for (int i=0; i<pts.size(); i++){
- if (pts.at(i).active == 1){
- p.x = pts.at(i).x;
- p.y = pts.at(i).y;
- p.active = 1;
- active.push_back(p);
- }
- }
- if (active.size() != 2){
- return false;
- }
- else{
- x = active.at(0).x + active.at(1).x;
- y = active.at(0).y + active.at(1).y;
- area_ret = x*y;
- }
- if (area_ret == area){
- return true;
- }
- return false;
- }
- void recursive(Retangulo r, vector<Retangulo> rets, vector<Ponto> pts, vector<Retangulo> colocados, int x_max, int y_max){
- int tam = pts.size();
- //Percorrer lista de cantos ativos
- for (int i=0; i<tam; i++){
- //Condição de paragem
- if (colocados.size() == nr_rets){
- //mostra_pts(pts);
- if (is_retangle(pts) == true){
- nr_configs++;
- }
- nr_configs++;
- return ;
- }
- if (pts.at(i).active == 1){
- Ponto p,p1;
- pts.at(i).active = 0;
- p.x = pts.at(i).x + r.x;
- p.y = pts.at(i).y;
- p1.x = pts.at(i).x;
- p1.y = pts.at(i).y + r.y;
- if (!check_retangle(colocados,r)){
- r.sup = p1;
- r.inf = p;
- //Verificar se existem interseções entre o retangulo a colocar e os ja colocados
- //if (!check_intersections(r,colocados)){
- cout << "Coloquei o " << r.id << " no ponto " << pts.at(i).x << "," << pts.at(i).y << "\n";
- colocados.push_back(r);
- p1.active = 1;
- p.active = 1;
- pts.push_back(p);
- pts.push_back(p1);
- //update_active_pts(pts);
- //}
- }
- cout << "Colocados: ";
- for (int c = 0; c<colocados.size(); c++){
- cout << colocados.at(c).id << " ";
- }
- cout << "\n";
- for (int j = 0; j<rets.size(); j++){
- if (!check_retangle(colocados,rets.at(j))){
- recursive(rets.at(j), rets, pts, colocados,0,0);
- }
- }
- }
- }
- }
- int main(){
- string line;
- string buf;
- int indices;
- int id = 1;
- int tam;
- cin >> indices;
- nr_rets = indices;
- vector<Retangulo> rets;
- vector<Retangulo> colocados;
- cin.ignore();
- while(indices != 0){
- getline(cin,line);
- Retangulo r;
- stringstream ss(line);
- vector<string> tokens;
- while (ss >> buf)
- tokens.push_back(buf);
- if (!tokens.empty()){
- istringstream convert(tokens.at(0));
- convert >> r.x;
- istringstream convert1(tokens.at(1));
- convert1 >> r.y;
- }
- r.id = id;
- rets.push_back(r);
- id++;
- indices--;
- }
- //Ponto na origem (0,0)
- vector<Ponto> pts;
- Ponto p;
- p.x = 0;
- p.y = 0;
- p.active = 1;
- pts.push_back(p);
- //Rodar retangulos
- tam = rets.size();
- for (int i=0; i<tam; i++){
- if (rets.at(i).x != rets.at(i).y){
- //Nao e quadrado, logo podemos rodar
- Retangulo r1;
- r1.x = rets.at(i).y;
- r1.y = rets.at(i).x;
- r1.id = rets.at(i).id;
- rets.push_back(r1);
- }
- }
- //Calculo da area total dos retangulos
- for (int i=0;i<rets.size();i++){
- area += (rets.at(i).x * rets.at(i).y);
- }
- for (int i=0; i<rets.size(); i++){
- recursive(rets.at(i),rets,pts,colocados,0,0);
- }
- cout << nr_configs;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement