-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMediumProgram.cs
More file actions
170 lines (150 loc) · 4.44 KB
/
MediumProgram.cs
File metadata and controls
170 lines (150 loc) · 4.44 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
169
170
using System;
using System.IO;
namespace hashcode
{
public class MediumProgram
{
// predetermined integers from input file
static int R = 200;
static int C = 250;
static int[,] inttop;
static bool isFirstTime = true;
static int slices = 0;
static int i = 0;
static int[,] sliceSaver = new int[6250, 4];
public static void Main (string[] args)
{
int score = 0;
//Instantiate a StreamReader to read from the text file.
StreamReader sr = new StreamReader (@"medium.in");
// gets the first role from input file out of the way
char[] row1 = sr.ReadLine ().ToCharArray();
// string that stores all the elements from the input file (T and M)
string toppings = sr.ReadToEnd ();
// character array
char[] chartop = toppings.ToCharArray();
// integer array that converts T to 1, M to 0
inttop = new int[R,C];
// filling inttop with values
int z=0;
for (int x=0;x<R;x++) {
for (int y=0;y<C;y++) {
if (chartop [z] == 'T') {
inttop [x, y] = 1;
} else if (chartop [z] == 'M')
inttop [x, y] = 0;
else{
y--;
}
z++;
}
}
// 2x6 slices
int slicesCut = slicer (0, 0, 1, 5, 4, 9);
slices = slicesCut + slices;
score = slicesCut * 12;
Console.WriteLine (slices + " " + score + "pts");
// 6x2 slices
slicesCut = slicer (0, 0, 5, 1, 4, 9);
slices = slicesCut + slices;
score = score + slicesCut * 12;
Console.WriteLine (slices + " " + score + "pts");
// 3x4 slices
slicesCut = slicer (0, 0, 2, 3, 4, 9);
slices = slicesCut + slices;
score = score + slicesCut * 12;
Console.WriteLine (slices + " " + score + "pts");
// 4x3 slices
slicesCut = slicer (0, 0, 3, 2, 4, 9);
slices = slicesCut + slices;
score = score + slicesCut * 12;
Console.WriteLine (slices + " " + score + "pts");
// 2x5 slices
slicesCut = slicer (0, 0, 1, 4, 4, 7);
slices = slicesCut + slices;
score = score + slicesCut * 10;
Console.WriteLine (slices + " " + score + "pts");
// 5x2 slices
slicesCut = slicer (0, 0, 4, 1, 4, 7);
slices = slicesCut + slices;
score = score + slicesCut * 10;
Console.WriteLine (slices + " " + score + "pts");
// 2x4 slices
slicesCut = slicer (0, 0, 1, 3, 4, 5);
slices = slicesCut + slices;
score = score + slicesCut * 8;
Console.WriteLine (slices + " " + score + "pts");
// 4x2 slices
slicesCut = slicer (0, 0, 3, 1, 4, 5);
slices = slicesCut + slices;
score = score + slicesCut * 8;
Console.WriteLine (slices + " " + score + "pts");
documenter();
Console.WriteLine ("DONE");
Console.ReadKey ();
}
// the method that creates the output file and writes to it
public static void documenter(){
Console.WriteLine ("Saving output file...");
for (int e=0;e<slices;e++) {
string line = sliceSaver[e,0] + " " + sliceSaver[e,1] + " " + sliceSaver[e,2] + " " + sliceSaver[e,3];
using (StreamWriter sw = new StreamWriter ("medium_output.txt", true)) {
if (isFirstTime) {
sw.WriteLine (slices);
isFirstTime = false;
}
sw.WriteLine (line);
}
}
}
// the method that cuts the pizza into suitable slices
public static int slicer(int r1, int c1, int r2, int c2, int min, int max){
int sliceCell = 0;
int slice = 0;
for (int r=0;r<R-r2;r++) {
for (int c=0;c<C-c2;c++) {
for (int x=r1+r;x<=r2+r;x++) {
for (int y=c1+c;y<=c2+c;y++) {
if (inttop [x, y] == 1) {
sliceCell++;
}
}
}
if (sliceCell < max) {
if (sliceCell >= min) {
bool isSlice = checker (r1 + r, c1 + c, r2 + r, c2 + c);
if (isSlice)
slice++;
}
}
sliceCell = 0;
}
}
return slice;
}
public static void saver(int r1, int c1, int r2, int c2) {
sliceSaver[i,0] = r1;
sliceSaver[i,1] = c1;
sliceSaver[i,2] = r2;
sliceSaver[i,3] = c2;
i++;
}
public static bool checker(int r1, int c1, int r2, int c2) {
bool check = true;
for (int a = 0; a <= i; a++) {
if (a == i)
break;
for (int r = r1; r <= r2; r++) {
for (int c = c1; c <= c2; c++) {
if ((sliceSaver [a, 0] <= r && r <= sliceSaver [a, 2]) && (sliceSaver [a, 1] <= c && c <= sliceSaver [a, 3])) {
check = false;
}
}
}
}
if (check)
saver (r1, c1, r2, c2);
return check;
}
}
}