Header.h
#pragma once
#include <vector>
#include <map>
#include <memory>
#ifndef __HEADER__
#define __HEADER__
class Point
{
private:
const int x;
const int y;
public:
Point(const int&, const int&);
int getPointX(void) const;
int getPointY(void) const;
};
class Square
{
private:
std::map<int, std::vector<Point>> points;
const Point lowerLeftPoint;
const Point higherRightPoint;
public:
Square(const int&, const int&, const int&, const int&);
void PointMaker(const Point&, const Point&);
auto getPoint(const int& key) const;
void remover(int, const std::vector<Point>::iterator);
Point getLowerLeftPoint(void) const;
Point getHigherRightPoint(void) const;
int CalculateArea(void) const;
std::shared_ptr<Square> newSquare(const std::shared_ptr<Square>);
~Square();
};
int testNumInput(void);
#endif
definition.cpp
#include "header.h"
#include <vector>
#include <memory>
Point::Point(const int& input_x, const int& input_y) : x(input_x), y(input_y) {}
int Point::getPointX(void) const { return x; }
int Point::getPointY(void) const { return y; }
Point Square::getHigherRightPoint() const {return higherRightPoint;}
Point Square::getLowerLeftPoint() const {return lowerLeftPoint;}
Square::Square(const int& point1_x, const int& point1_y, const int& point2_x, const int& point2_y)
:lowerLeftPoint(point1_x, point1_y), higherRightPoint(point2_x, point2_y)
{
Point point1(point1_x, point1_y);
Point point2(point2_x, point2_y);
PointMaker(point1, point2);
}
void Square::PointMaker(const Point& lowerLeftPoint, const Point& higherRightPoint)
{
for (int i = lowerLeftPoint.getPointX(); i < higherRightPoint.getPointX(); i++)
{
for (int j = higherRightPoint.getPointY(); j < higherRightPoint.getPointY(); j++)
{
Point point(i, j);
points[i].push_back(point);
}
}
}
void Square::remover(int x, const std::vector<Point>::iterator y)
{
points[x].erase(y);
}
auto Square::getPoint(const int& key) const {return points.at(key);}
Square::~Square()
{
points.clear();
}
std::shared_ptr<Square> Square::newSquare(const std::shared_ptr<Square> addedSquare)
{
std::vector<std::unique_ptr<Point>> overlappingPoints;
if (lowerLeftPoint.getPointX() >= addedSquare->getHigherRightPoint().getPointX() ||
higherRightPoint.getPointX() <= addedSquare->getLowerLeftPoint().getPointX() ||
lowerLeftPoint.getPointY() >= addedSquare->getHigherRightPoint().getPointY() ||
higherRightPoint.getPointY() <= addedSquare->getLowerLeftPoint().getPointY())
return nullptr; //this case the two squares are not related in any theoratical way
else
{
for (auto it = points.begin(); it != points.end(); it++)
{
if (!addedSquare->getPoint(it->first).empty()) //if in the new square there is an x value same as the current square
{
for (auto it2 = points.at(it->first).begin(); it2 != points.at(it->first).end(); it2++)
{
if (std::find(addedSquare->getPoint(it->first).begin(), addedSquare->getPoint(it->first).end(),
[](std::unique_ptr<Point> point) {return point->getPointY(); }) != addedSquare->getPoint(it->first).end())
{
overlappingPoints.push_back(std::make_unique<Point>(it->first, it2->getPointY()));
remover(it->first, it2);
addedSquare->remover(it->first, it2);
}
}
}
}
return std::make_shared<Square>(overlappingPoints.front()->getPointX(),
overlappingPoints.front()->getPointY(),
overlappingPoints.back()->getPointX(),
overlappingPoints.back()->getPointY());
}
}
int Square::CalculateArea() const
{
int area = 0;
for (auto it = points.begin(); it != points.end(); it++)
{
area += it->second.size();
}
return area;
}
Source.cpp
#include <iostream>
#include <cstdio>
#include <vector>
#include <memory>
#include "Header.h"
#include <algorithm>
//#include <gtest/gtest.h>
int main(int argc, char ** argv)
{
//testing::InitGoogleTest(&argc, argv);
int testNum = 0;
int numOfSquares = 0;
std::vector<std::shared_ptr<Square>> squares;
std::vector<int> area;
std::vector<int> results;
testNum = testNumInput();
for (int i = 0; i < testNum; i++)
{
scanf("%d", &numOfSquares);
for (int j = 0, pos1_x, pos1_y, pos2_x, pos2_y; j < numOfSquares; j++)
{
scanf("%d %d %d %d", &pos1_x, &pos1_y, &pos2_x, &pos2_y);
squares.push_back(std::make_shared<Square>(pos1_x, pos1_y, pos2_x, pos2_y));
for (int k=0; k!=squares.size(); k++)
{
std::shared_ptr<Square> divisionResult;;
if (divisionResult != nullptr)
squares.push_back(divisionResult);
}
}
for (int j = 0; j < squares.size(); j++)
area.push_back(squares.at(j)->CalculateArea());
results.push_back(squares.size());
results.push_back(*std::max_element(area.begin(), area.end()));
}
for(auto it=results.begin(); it!= results.end(); it += 2)
{
std::cout << *it << " " << *(it + 1) << std::endl;
}
//return RUN_ALL_TESTS();
return 0;
}
int testNumInput(void)
{
int testNum;
do //test number input and checking loop
{
std::cin >> testNum;
if (testNum > 10)
std::cout << "test case number must be less than 10";
} while (testNum > 10);
return testNum;
}
이게 gcc 에러 코드
Point& Point::operatr= 이 에러가 어디서 왜 발생하는지 잘 모르겠음
보니까 Point의 const 상태나 생성자 쪽에 문제가 있는 것 같은데..
ㄴ 넵넵
헐 저도 여기저기 const 제거해봤는데 x,y 를 안 해봤네요
정말 감사합니다
가끔가다가 딱 필요한게 있는데 (make_shared/unique) C++14 에 있어서 어쩌다가 올라갔습니다 ㅋㅋ