Skip to content

Commit 39a008b

Browse files
committed
added some improvements before hashcode 2019
1 parent 822f188 commit 39a008b

File tree

8 files changed

+218
-234
lines changed

8 files changed

+218
-234
lines changed

README.md

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# TeamPizza
2-
Google hashcode task
3-
Pizza
4-
Practice Problem for Hash Code 2017
52

6-
(Project contains all examples and description is pdf format)
3+
### Practice Problem for Google Hash Code 2017
74

85
### Introduction
6+
97
Did you know that at any given time, someone is cutting pizza somewhere around the world? The decision
108
about how to cut the pizza sometimes is easy, but sometimes it’s really hard: you want just the right amount
119
of tomatoes and mushrooms on each slice. If only there was a way to solve this problem using technology...
@@ -41,27 +39,18 @@ lines terminated with a single ‘\n’ character at the end of each line (UNIX-
4139

4240
The file consists of:
4341
- one line containing the following natural numbers separated by single spaces:
44-
- R (1 ≤ R ≤ 1000) is the number of rows
45-
- C (1 ≤ C ≤ 1000) is the number of columns
46-
- L (1 ≤ L ≤ 1000) is the minimum number of each ingredient cells in a slice
47-
- H (1 ≤ H ≤ 1000) is the maximum total number of cells of a slice
48-
49-
*Google 2017, All rights reserved.*
42+
- R (1 ≤ R ≤ 1000) is the number of rows
43+
- C (1 ≤ C ≤ 1000) is the number of columns
44+
- L (1 ≤ L ≤ 1000) is the minimum number of each ingredient cells in a slice
45+
- H (1 ≤ H ≤ 1000) is the maximum total number of cells of a slice
5046

51-
R lines describing the rows of the pizza (one after another). Each of these lines contains C
47+
- R lines describing the rows of the pizza (one after another). Each of these lines contains C
5248
characters describing the ingredients in the cells of the row (one cell after another). Each character
5349
is either ‘M’ (for mushroom) or ‘T’ (for tomato).
5450

5551
**Example**
5652

57-
- 3 5 1 6
58-
- TTTTT
59-
- TMMMT
60-
- TTTTT
61-
62-
3 rows, 5 columns, min 1 of each ingredient per slice, max 6 cells per slice
63-
64-
**Example input file.**
53+
![alt](images/Screenshot.png)
6554

6655
**Submissions**
6756

@@ -72,23 +61,11 @@ The file must consist of:
7261
- U lines describing the slices. Each of these lines must contain the following natural numbers separated by single spaces:
7362
- r 1 , c 1 , r 2 , c 2 describe a slice of pizza delimited by the rows r (0 ≤ r1,r2 < R, 0 ≤ c1, c2 < C) 1 and r 2 and the columns c 1 and c 2 , including the cells of the delimiting rows and columns. The rows ( r 1 and r 2 ) can be given in any order. The columns ( c 1 and c 2 ) can be given in any order too.
7463

75-
**Example**
76-
77-
- 0 0 2 1
78-
- 0 2 2 2
79-
- 0 3 2 4
8064

81-
3 slices.
82-
- First slice between rows (0,2) and columns (0,1).
83-
- Second slice between rows (0,2) and columns (2,2).
84-
- Third slice between rows (0,2) and columns (3,4).
85-
- Example submission file.
86-
87-
*© Google 2017, All rights reserved.*
88-
89-
Slices described in the example submission file marked in green, orange and purple.
65+
![alt](images/Screenshot_1.png)
9066

9167
### Validation
68+
9269
For the solution to be accepted:
9370
- the format of the file must match the description above,
9471
- each cell of the pizza must be included in at most one slice,
@@ -97,9 +74,11 @@ For the solution to be accepted:
9774
- total area of each slice must be at most H
9875

9976
### Scoring
77+
10078
The submission gets a score equal to the total number of cells in all slices.
10179
Note that there are multiple data sets representing separate instances of the problem. The final
10280
score for your team is the sum of your best scores on the individual data sets.
10381

10482
### Scoring example
83+
10584
The example submission file given above cuts the slices of 6, 3 and 6 cells, earning 6 + 3 + 6 = 15 points.

images/Screenshot.png

82.2 KB
Loading

images/Screenshot_1.png

115 KB
Loading

src/main/java/com/team/hashcode/pizza/BestPosition.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ public BestPosition(int rows, int columns, char[][] matrixCopy) {
1616
findBestPositions();
1717
}
1818

19-
private void findBestPositions(){
20-
finish : for(int c = 0; c < columns; c++) {
19+
private void findBestPositions() {
20+
finish:
21+
for (int c = 0; c < columns; c++) {
2122
for (int r = 0; r < rows; r++) {
22-
if('*' != matrixCopy[r][c]){
23+
if ('*' != matrixCopy[r][c]) {
2324
bestRow = r;
2425
bestColumn = c;
2526
break finish;

src/main/java/com/team/hashcode/pizza/MinEachIngredient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public MinEachIngredient(int minEachIngredient) {
1010
this.minEachIngredient = minEachIngredient;
1111
}
1212

13-
public boolean isEnough(char ingredient){
13+
public boolean isEnough(char ingredient) {
1414
final int tomato = array[0];
1515
final int mushrooms = array[1];
16-
if('T' == ingredient){
16+
if ('T' == ingredient) {
1717
array[0] = tomato + 1;
18-
}else if('M' == ingredient){
18+
} else if ('M' == ingredient) {
1919
array[1] = mushrooms + 1;
2020
}
2121
return array[0] >= minEachIngredient && array[1] >= minEachIngredient;

src/main/java/com/team/hashcode/pizza/Pizza.java

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,49 @@
1717
* */
1818
public class Pizza {
1919

20-
private char[][] matrix;
21-
private char[][] matrixCopy;
22-
private int maxCellsPerSlice;
20+
private char[][] matrix;
21+
private char[][] matrixCopy;
22+
private int maxCellsPerSlice;
2323
private int minEachIngredient;
2424
private final List<Slice> slices = new ArrayList<>();
2525

26-
private int rows;
27-
private int columns;
26+
private int rows;
27+
private int columns;
2828

29-
public Pizza(char[][] matrix, int maxCellsPerSlice, int minEachIngredient) {
30-
this.matrix = matrix;
31-
this.matrixCopy = matrix;
32-
this.maxCellsPerSlice = maxCellsPerSlice;
29+
public Pizza(char[][] matrix, int maxCellsPerSlice, int minEachIngredient) {
30+
this.matrix = matrix;
31+
this.matrixCopy = matrix;
32+
this.maxCellsPerSlice = maxCellsPerSlice;
3333
this.minEachIngredient = minEachIngredient;
34-
this.rows = matrix.length;
35-
this.columns = matrix[0].length;
36-
}
34+
this.rows = matrix.length;
35+
this.columns = matrix[0].length;
36+
}
3737

38-
public void cutPizza(){
38+
public void cutPizza() {
3939
cutPizza(0, 0);
4040
}
4141

42-
private void cutPizza(int r, int c){
43-
for(; c < columns; c++) {
44-
for(; r < rows; r++){
45-
if(isCorrectSlice(c, r)) {
42+
private void cutPizza(int r, int c) {
43+
for (; c < columns; c++) {
44+
for (; r < rows; r++) {
45+
if (isCorrectSlice(c, r)) {
4646
createSlices(c, r);
47-
}else{
47+
} else {
4848
final BestPosition bestPosition = new BestPosition(rows, columns, matrixCopy);
4949
createSlicesByBestPositions(bestPosition.getBestRow(), bestPosition.getBestColumn());
5050
}
51-
}
52-
}
51+
}
52+
}
5353
}
5454

55-
private void createSlices(int c, int r){
56-
for(int c1 = c; c1 < columns; ) {
55+
private void createSlices(int c, int r) {
56+
for (int c1 = c; c1 < columns; ) {
5757
final Slice slice = new Slice(maxCellsPerSlice);
58-
for(int r1 = r; r1 < rows; r1++) {
59-
if(slice.getArraySize() < maxCellsPerSlice) {
58+
for (int r1 = r; r1 < rows; r1++) {
59+
if (slice.getArraySize() < maxCellsPerSlice) {
6060
slice.addIngredient(matrix[r1][c1], r1, c1);
6161
matrixCopy[r1][c1] = '*';
62-
}else{
62+
} else {
6363
c1++;
6464
slices.add(slice);
6565
break;
@@ -68,83 +68,87 @@ private void createSlices(int c, int r){
6868
}
6969
}
7070

71-
private void createSlicesByBestPositions(int bestRow, int bestColumn){
71+
private void createSlicesByBestPositions(int bestRow, int bestColumn) {
7272
int rows = bestRow + 1;
73-
if(rows == this.rows){ // then it bottom
74-
for(; bestColumn < columns; bestColumn++) {
75-
if(isCorrectSliceByColumns(bestColumn, bestRow)){
73+
if (rows == this.rows) { // then it bottom
74+
for (; bestColumn < columns; bestColumn++) {
75+
if (isCorrectSliceByColumns(bestColumn, bestRow)) {
7676
fillSliceAndMatrixCopyByColumns(bestColumn, bestRow);
7777
}
7878
}
7979
}
8080
}
8181

82-
private void fillSliceAndMatrixCopyByColumns(int c, int r){
82+
private void fillSliceAndMatrixCopyByColumns(int c, int r) {
8383
final Slice slice = new Slice(maxCellsPerSlice);
84-
for(int c1 = c; c1 < columns; c1++) {
85-
if(slice.getArraySize() < maxCellsPerSlice) {
84+
for (int c1 = c; c1 < columns; c1++) {
85+
if (slice.getArraySize() < maxCellsPerSlice) {
8686
slice.addIngredient(matrix[r][c1], r, c1);
8787
matrixCopy[r][c1] = '*';
88-
}else{
88+
} else {
8989
break;
9090
}
9191
}
9292
slices.add(slice);
9393
}
9494

95-
private boolean isCorrectSlice(int c, int r){
95+
private boolean isCorrectSlice(int c, int r) {
9696
int sliceSize = 0;
9797
boolean isEnoughIngredient = false;
9898
final MinEachIngredient eachIngredient = new MinEachIngredient(minEachIngredient);
99-
for(; c < columns; c++) {
100-
if(r < matrix.length - 1 && c < matrix[0].length - 1) {
99+
for (; c < columns; c++) {
100+
if (r < matrix.length - 1 && c < matrix[0].length - 1) {
101101
for (; r < rows; r++) {
102102
char ingredient = matrix[r][c];
103103
if (sliceSize < maxCellsPerSlice) {
104-
if(eachIngredient.isEnough(ingredient)){
104+
if (eachIngredient.isEnough(ingredient)) {
105105
isEnoughIngredient = true;
106106
}
107-
if (sliceSize >= maxCellsPerSlice && !isEnoughIngredient)
107+
if (sliceSize >= maxCellsPerSlice && !isEnoughIngredient) {
108108
return false;
109-
if ('*' == matrixCopy[r][c])
109+
}
110+
if ('*' == matrixCopy[r][c]) {
110111
return false;
112+
}
111113
sliceSize++;
112114
} else {
113115
return true;
114116
}
115117
}
116-
}else{
118+
} else {
117119
break;
118120
}
119121
}
120122
return false;
121123
}
122124

123-
private boolean isCorrectSliceByColumns(int c, int r){
125+
private boolean isCorrectSliceByColumns(int c, int r) {
124126
int sliceSize = 1;
125127
boolean isEnoughIngredient = false;
126128
final MinEachIngredient eachIngredient = new MinEachIngredient(minEachIngredient);
127-
for(; c < columns; c++) {
129+
for (; c < columns; c++) {
128130
char ingredient = matrix[r][c];
129-
if(sliceSize <= maxCellsPerSlice) {
130-
if(eachIngredient.isEnough(ingredient)){
131+
if (sliceSize <= maxCellsPerSlice) {
132+
if (eachIngredient.isEnough(ingredient)) {
131133
isEnoughIngredient = true;
132134
}
133-
if(sliceSize >= maxCellsPerSlice && !isEnoughIngredient)
135+
if (sliceSize >= maxCellsPerSlice && !isEnoughIngredient) {
134136
return false;
135-
if('*' == matrixCopy[r][c])
137+
}
138+
if ('*' == matrixCopy[r][c]) {
136139
return false;
140+
}
137141
sliceSize++;
138-
}else {
142+
} else {
139143
return true;
140144
}
141145
}
142146
return true;
143147
}
144148

145-
public List<Slice> getSlices(){
146-
return slices;
147-
}
149+
public List<Slice> getSlices() {
150+
return slices;
151+
}
148152

149153
public char[][] getMatrixCopy() {
150154
return matrixCopy;

src/main/java/com/team/hashcode/pizza/Slice.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,46 @@
44

55
public class Slice {
66

7-
private char[] slicePositions;
8-
private int arraySize;
9-
private int[] rows;
10-
private int[] columns;
11-
12-
public Slice(int sizeSlice) {
13-
this.slicePositions = new char[sizeSlice];
14-
this.rows = new int[sizeSlice];
15-
this.columns = new int[sizeSlice];
16-
}
17-
18-
public void addIngredient(char value, int r, int c){
19-
slicePositions[arraySize] = value;
20-
rows[arraySize] = r;
21-
columns[arraySize] = c;
22-
arraySize++;
23-
}
24-
25-
public int getRowFirst(){
26-
return rows[0];
27-
}
28-
29-
public int getColumnsFirst(){
30-
return columns[0];
31-
}
32-
33-
public int getRowLast(){
34-
return rows[rows.length - 1];
35-
}
36-
public int getColumnsLast(){
37-
return columns[columns.length - 1];
38-
}
39-
40-
public int getArraySize(){
41-
return arraySize;
42-
}
43-
44-
@Override
45-
public String toString() {
46-
return Arrays.toString(slicePositions);
47-
}
7+
private char[] slicePositions;
8+
private int arraySize;
9+
private int[] rows;
10+
private int[] columns;
11+
12+
public Slice(int sizeSlice) {
13+
this.slicePositions = new char[sizeSlice];
14+
this.rows = new int[sizeSlice];
15+
this.columns = new int[sizeSlice];
16+
}
17+
18+
public void addIngredient(char value, int r, int c) {
19+
slicePositions[arraySize] = value;
20+
rows[arraySize] = r;
21+
columns[arraySize] = c;
22+
arraySize++;
23+
}
24+
25+
public int getRowFirst() {
26+
return rows[0];
27+
}
28+
29+
public int getColumnsFirst() {
30+
return columns[0];
31+
}
32+
33+
public int getRowLast() {
34+
return rows[rows.length - 1];
35+
}
36+
37+
public int getColumnsLast() {
38+
return columns[columns.length - 1];
39+
}
40+
41+
public int getArraySize() {
42+
return arraySize;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return Arrays.toString(slicePositions);
48+
}
4849
}

0 commit comments

Comments
 (0)