-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPizzaProblem.cs
292 lines (251 loc) · 9.97 KB
/
PizzaProblem.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace pizzaSolution
{
public class PizzaProblem
{
// string dataFile = "a_example.in";
// string dataFile = "b_small.in";
// string dataFile = "c_medium.in";
string dataFile = "d_big.in";
// string resultFile = "a_result.txt";
// string resultFile = "b_result.txt";
// string resultFile = "c_result.txt";
string resultFile = "d_result.txt";
int rowCount;
int columnCount;
int minimumNumberOfEachIngredient;
int maxNumberOfCells;
int mushroomCount = 0;
int tomatoeCount = 0;
public async Task Main()
{
var foundSlices = new List<PizzaSlice>();
var data = await ReadDataAsync();
var ingredientToSearch = DetermineWhichIngredientIsLeast(mushroomCount, tomatoeCount);
var sliceUsageData = CreateMatrixOfUnusedCells(data);
var emptyCycles = 0;
while (true)
{
var pointToUse = GetUnusedStartingPoint(data, sliceUsageData, ingredientToSearch);
if (pointToUse.Y == -1)
break; // we didn't find the next Point
var shapes = GeneratePossibleShapes(maxNumberOfCells, minimumNumberOfEachIngredient);
var fittingShape = FindFirstFittingShape(pointToUse, data, sliceUsageData, shapes);
if (fittingShape is null) {
emptyCycles++;
if (emptyCycles > 10000) {
break;
}
continue; // no possible shapes here
}
emptyCycles = 0;
foundSlices.Add(new PizzaSlice
{
X1 = pointToUse.X - fittingShape.Height + 1,
Y1 = pointToUse.Y - fittingShape.Width + 1,
X2 = pointToUse.X,
Y2 = pointToUse.Y
});
MarkSliceAsTaken(pointToUse, sliceUsageData, fittingShape);
}
await PrintResult(foundSlices);
}
private async Task PrintResult(List<PizzaSlice> slices)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), resultFile);
using (var fileStream = new FileStream(path, FileMode.Create))
using (var streamWriter = new StreamWriter(fileStream, Encoding.ASCII, 1024, true))
{
await streamWriter.WriteLineAsync (slices.Count.ToString ());
for (int i = 0; i < slices.Count; i++) {
var slice = slices[i];
await streamWriter.WriteLineAsync ($"{slice.X1} {slice.Y1} {slice.X2} {slice.Y2}");
}
}
}
private void MarkSliceAsTaken(Point pointToUse, List<List<bool>> sliceUsageData, Shape fittingShape)
{
for (int i = pointToUse.X; i >= pointToUse.X - fittingShape.Height + 1; i--)
{
for (int j = pointToUse.Y; j >= pointToUse.Y - fittingShape.Width + 1; j--)
{
sliceUsageData[i][j] = true;
}
}
}
private Shape FindFirstFittingShape(Point startingPoint,
List<List<PizzaIngredient>> data,
List<List<bool>> sliceUsageData,
List<Shape> shapes)
{
for (var i = 0; i < shapes.Count; i++)
{
var shape = shapes[i];
if (startingPoint.Y - shape.Width + 1 >= 0 &&
startingPoint.X - shape.Height + 1 >= 0)
{
var hasIngredients = SpaceHasIngredientCountNecessary(startingPoint, data, shape, minimumNumberOfEachIngredient);
if (!hasIngredients)
continue;
var cellsAreFree = CheckIfCellsAreFree(startingPoint, shape, sliceUsageData);
if (!cellsAreFree)
continue;
return shape;
}
}
return null;
}
private bool CheckIfCellsAreFree(Point startingPoint, Shape shape, List<List<bool>> sliceUsageData)
{
for (int i = startingPoint.X; i >= startingPoint.X - shape.Height + 1; i--)
{
for (int j = startingPoint.Y; j >= startingPoint.Y - shape.Width + 1; j--)
{
if (sliceUsageData[i][j] == true)
{
return false; // slice is already taken
}
}
}
return true;
}
private bool SpaceHasIngredientCountNecessary(
Point startPoint,
List<List<PizzaIngredient>> data,
Shape shape,
int minimumNumberOfEachIngredient)
{
int mushroomCOunt = 0;
int tomatoeCount = 0;
for (int i = startPoint.X; i >= startPoint.X - shape.Height + 1; i--)
{
for (int j = startPoint.Y; j >= startPoint.Y - shape.Width + 1; j--)
{
if (data[i][j].Ingredient == Ingredient.Mushroom)
{
mushroomCOunt++;
}
else
{
tomatoeCount++;
}
}
}
if (mushroomCOunt >= minimumNumberOfEachIngredient &&
tomatoeCount >= minimumNumberOfEachIngredient)
{
return true;
}
return false;
}
private List<Shape> GeneratePossibleShapes(int maxNumberOfCells, int minimumNumberOfEachIngredient)
{
var shapes = new List<Shape>();
var minimumCellCount = minimumNumberOfEachIngredient * 2;
for (var i = 1; i <= maxNumberOfCells; i++)
{
for (var j = 1; j <= maxNumberOfCells; j++)
{
var plotas = i * j;
if (plotas > maxNumberOfCells)
break;
if (plotas < minimumCellCount)
continue;
shapes.Add(new Shape(i, j));
}
}
shapes.Sort (new ShapeComparer ());
return shapes;
}
private Point GetUnusedStartingPoint(List<List<PizzaIngredient>> data, List<List<bool>> usageData, Ingredient ingredientToSearchFor)
{
var rnd = new Random ();
var iIndex = rnd.Next (0, data.Count - 1);
var jIndex = rnd.Next (0, data[0].Count - 1);
for (var i = iIndex; i < data.Count; i++)
{
for (var j = jIndex; j < data[0].Count; j++)
{
if (data[i][j].Ingredient == ingredientToSearchFor &&
usageData[i][j] == false)
{
return new Point(i, j);
}
}
}
for (var i = iIndex - 1; i >= 0; i--)
{
for (var j = jIndex - 1; j >= 0; j--)
{
if (data[i][j].Ingredient == ingredientToSearchFor &&
usageData[i][j] == false)
{
return new Point(i, j);
}
}
}
return new Point(-1, -1);
}
private List<List<bool>> CreateMatrixOfUnusedCells(List<List<PizzaIngredient>> data)
{
var usageList = new List<List<bool>>();
data.ForEach((row) =>
{
var usageRow = new List<bool>();
row.ForEach(ingredient =>
{
usageRow.Add(false);
});
usageList.Add(usageRow);
});
return usageList;
}
private Ingredient DetermineWhichIngredientIsLeast(int mushrooms, int tomatoes)
{
if (mushrooms < tomatoes)
return Ingredient.Mushroom;
return Ingredient.Tomatoe;
}
private async Task<List<List<PizzaIngredient>>> ReadDataAsync()
{
var data = new List<List<PizzaIngredient>>();
var path = Path.Combine(Directory.GetCurrentDirectory(), "data", dataFile);
// R C L H
using (var fileStream = new FileStream(path, FileMode.Open))
using (var streamReader = new StreamReader(fileStream))
{
var firstLine = await streamReader.ReadLineAsync();
var chars = firstLine.Split(' ');
int.TryParse(chars[0], out rowCount);
int.TryParse(chars[1], out columnCount);
int.TryParse(chars[2], out minimumNumberOfEachIngredient);
int.TryParse(chars[3], out maxNumberOfCells);
while (streamReader.Peek() != -1)
{
var dataLine = await streamReader.ReadLineAsync();
var ingredientLine = new List<PizzaIngredient>();
for (var i = 0; i < dataLine.Length; i++)
{
if (dataLine[i] == 'M')
{
ingredientLine.Add(new PizzaIngredient(Ingredient.Mushroom));
mushroomCount++;
}
else if (dataLine[i] == 'T')
{
ingredientLine.Add(new PizzaIngredient(Ingredient.Tomatoe));
tomatoeCount++;
}
}
data.Add(ingredientLine);
}
}
return data;
}
}
}