This repository was archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizzaSeparator.cpp
More file actions
168 lines (148 loc) · 4.05 KB
/
PizzaSeparator.cpp
File metadata and controls
168 lines (148 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "PizzaSeparator.h"
#include "PrintVector.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Point.h"
#include "Parser.h"
using namespace std;
PizzaSeparator::PizzaSeparator(std::vector<std::vector<bool>>* v){
for(unsigned int i = 0; i < columns; ++i){
isTaken.emplace_back();
for(unsigned int j = 0; j < rows; ++j){
isTaken[i].push_back(false);
}
}
toSlice=v;
}
void PizzaSeparator::SlicePizza(){
int counter=0;
while((startingP=FindStartingPoint()).IsValid()){
endP=startingP;
if((*toSlice)[startingP.GetX()][startingP.GetY()])actMus++;
else actTom++;
while(actTom<::min or actMus<::min or FieldSize()>::max){
if(ExpandVertically());
else if(!ExpandHorizontally()){
if(FieldSize()<=::max){
isTaken[startingP.GetX()][startingP.GetY()]=true;
break;
}
}
if(FieldSize()>::max){
if(!ExpandHorizontally()){
isTaken[startingP.GetX()][startingP.GetY()]=true;
break;
}
}
while(FieldSize()>::max)DecreaseVertically();
if(FieldSize()<=1){
isTaken[startingP.GetX()][startingP.GetY()]=true;
break;
}
}
if(actMus>=::min and actTom>=::min and endP.IsValid() and startingP.IsValid() and FieldSize()<=::max){
SetTaken();
AddToResults();
counter++;
}
if(counter%500==0)cout<<counter<<endl;
actMus=0;
actTom=0;
}
}
PizzaSeparator::~PizzaSeparator(){
delete toSlice;
}
Point PizzaSeparator::FindStartingPoint(){
for(unsigned int i = 0; i < columns; ++i){
for(unsigned int j = 0 ; j < rows; ++j){
if(isTaken[i][j]==0) return Point(i, j, validity::valid);
}
}
return Point(0,0,validity::notvalid);
}
bool PizzaSeparator::IsValid(const Point& upperLeft, const Point& lowerRight)
{
if(lowerRight.GetX()>=columns or lowerRight.GetY()>=rows)return false;
for (unsigned int i=upperLeft.GetX(); i<=lowerRight.GetX(); ++i)
{
for (unsigned int j=upperLeft.GetY(); j<=lowerRight.GetY(); ++j)
{
if (isTaken[i][j]==true)
return false;
}
}
return true;
}
unsigned int PizzaSeparator::FieldSize(){
return (endP.GetX()-startingP.GetX()+1)*(endP.GetY()-startingP.GetY()+1);
}
bool PizzaSeparator::ExpandVertically(){
if((IsValid( Point(startingP.GetX(),endP.GetY()) , Point(endP.GetX() , endP.GetY() + 1) ) ) and endP.GetY() + 1 < rows){
endP.SetY( endP.GetY() + 1);
for(unsigned int i = startingP.GetX(); i <= endP.GetX(); ++i){
if((*toSlice)[i][endP.GetY()]==0)actTom++;
else actMus++;
}
return true;
}
return false;
}
bool PizzaSeparator::ExpandHorizontally(){
if((IsValid(Point(endP.GetX(),startingP.GetY() ) , Point(endP.GetX() +1 , endP.GetY() ) ) )and endP.GetX() + 1 < columns){
endP.SetX( endP.GetX() + 1);
for(unsigned int j = startingP.GetY(); j <= endP.GetY(); ++j){
if((*toSlice)[endP.GetX()][j]==0)actTom++;
else actMus++;
}
return true;
}
return false;
}
void PizzaSeparator::SetTaken(){
for(unsigned int i = startingP.GetX(); i<=endP.GetX() ; ++i){
for(unsigned int j = startingP.GetY(); j<=endP.GetY(); ++j){
isTaken[i][j]=1;
}
}
}
void PizzaSeparator::DecreaseVertically(){
for(unsigned int i = startingP.GetX(); i <= endP.GetX(); ++i){
if((*toSlice)[i][endP.GetY()]==0)actTom--;
else actMus--;
}
endP.SetY( endP.GetY() - 1);
}
void PizzaSeparator::AddToResults()
{
auto added = std::make_pair(startingP, endP);
m_result.push_back(added);
}
void PizzaSeparator::PrintResults()
{
for (std::vector<std::pair<Point, Point>>::iterator i = m_result.begin(); i!=m_result.end(); ++i)
{
std::cout<<(*i).first.GetY()<<" ";
std::cout<<(*i).first.GetX()<<" ";
std::cout<<(*i).second.GetY()<<" ";
std::cout<<(*i).second.GetX()<<std::endl;
}
}
void PizzaSeparator::SaveToFile()
{
std::ofstream outputFile("medium.out");;
if (!outputFile.is_open())
{
std::cout<<"Could not open a file"<<std::endl;
exit(-1);
}
outputFile<<m_result.size()<<std::endl;
for (std::vector<std::pair<Point, Point>>::iterator i = m_result.begin(); i!=m_result.end(); ++i)
{
outputFile<<(*i).first.GetY()<<" ";
outputFile<<(*i).first.GetX()<<" ";
outputFile<<(*i).second.GetY()<<" ";
outputFile<<(*i).second.GetX()<<std::endl;
}
}