22
33public class DFSMethods {
44
5+ public int amoluntOfValidCellsAroundSlice (List <Cell > slice , List <Cell > pizza ){
6+ int cellsAroundSlice = 0 ;
7+ //found coordinates of slice angles.
8+ int minX = findMinX (slice );
9+ int minY = findMinY (slice );
10+ int maxX = findMaxX (slice );
11+ int maxY = findMaxY (slice );
12+
13+ //generate arrays of x and y coordinates for this slice.
14+ List <Integer > differenceXCoordinates = fillArrayWithIntValues (minX , maxX );
15+ List <Integer > differenceYCoordinates = fillArrayWithIntValues (minY , maxY );
16+
17+ //checking top cells.
18+ if (didPizzaContainsAllNeededHorizontalCells (pizza , differenceXCoordinates , minY - 1 )) cellsAroundSlice += differenceXCoordinates .size ();
19+
20+ //checking left cells.
21+ if (didPizzaContainsAllNeededVerticalCells (pizza , differenceYCoordinates , maxX + 1 )) cellsAroundSlice += differenceYCoordinates .size ();
22+
23+ //checking bottom cells.
24+ if (didPizzaContainsAllNeededHorizontalCells (pizza , differenceXCoordinates , maxY + 1 )) cellsAroundSlice += differenceXCoordinates .size ();
25+
26+ //checking rirht cells.
27+ if (didPizzaContainsAllNeededVerticalCells (pizza , differenceYCoordinates , minX - 1 )) cellsAroundSlice += differenceYCoordinates .size ();
28+
29+ return cellsAroundSlice ;
30+ }
31+
532 public boolean stepUp (List <Cell > slice , List <Cell > pizza , int maxCellsInSlice ){
6- // empty list difference X coordinates in slice for using it further.
7- List <Integer > differenceXCoordinates = new ArrayList <Integer >();
33+
834 // finding min y coordinate in slice and fill values to differenceXCoordinates.
9- Integer minYCoordInSlice = findMinYAndFillAllDifferenceX (slice , differenceXCoordinates );
35+ Integer minYCoordInSlice = findMinY (slice );
36+ List <Integer > differenceXCoordinates = fillAllDifferenceX (slice );
1037
1138 //prevent this method for cutting too big slice.
1239 if (slice .size () + differenceXCoordinates .size () > maxCellsInSlice ) return false ;
@@ -30,8 +57,8 @@ public boolean stepUp(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice){
3057
3158 public boolean stepRight (List <Cell > slice , List <Cell > pizza , int maxCellsInSlice ){
3259
33- List < Integer > differenceYCoordinates = new ArrayList < Integer >( );
34- Integer maxXCoordInSlice = findMaxXAndFillAllDifferenceY (slice , differenceYCoordinates ) ;
60+ Integer maxXCoordInSlice = findMaxX ( slice );
61+ List < Integer > differenceYCoordinates = fillAllDifferenceY (slice ); ;
3562
3663 if (slice .size () + differenceYCoordinates .size () > maxCellsInSlice ) return false ;
3764
@@ -50,8 +77,8 @@ public boolean stepRight(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice
5077
5178 public boolean stepDown (List <Cell > slice , List <Cell > pizza , int maxCellsInSlice ){
5279
53- List < Integer > differenceXCoordinates = new ArrayList < Integer >( );
54- Integer maxYCoordInSlice = findMaxYAndFillAllDifferenceX (slice , differenceXCoordinates );
80+ Integer maxYCoordInSlice = findMaxY ( slice );
81+ List < Integer > differenceXCoordinates = fillAllDifferenceX (slice );
5582
5683 if (slice .size () + differenceXCoordinates .size () > maxCellsInSlice ) return false ;
5784
@@ -70,8 +97,8 @@ public boolean stepDown(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice)
7097
7198 public boolean stepLeft (List <Cell > slice , List <Cell > pizza , int maxCellsInSlice ){
7299
73- List < Integer > differenceYCoordinates = new ArrayList < Integer >( );
74- Integer minXCoordInSlice = findMinXAndFillAllDifferenceY (slice , differenceYCoordinates );
100+ Integer minXCoordInSlice = findMinX ( slice );
101+ List < Integer > differenceYCoordinates = fillAllDifferenceY (slice );
75102
76103 if (slice .size () + differenceYCoordinates .size () > maxCellsInSlice ) return false ;
77104
@@ -88,12 +115,29 @@ public boolean stepLeft(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice)
88115
89116 }
90117
91- private int findMinYAndFillAllDifferenceX ( List <Cell > slice , List <Integer > diffX ){
92- Integer minYCoord = null ;
118+ private List <Integer > fillAllDifferenceX ( List <Cell > slice ){
119+ List < Integer > diffX = new ArrayList < Integer >() ;
93120 for (Cell cell : slice ){
94121 if (!diffX .contains (cell .x )){
95122 diffX .add (cell .x );
96123 }
124+ }
125+ return diffX ;
126+ }
127+
128+ private List <integer > fillAllDifferenceY (List <Cell > slice ){
129+ List <Integer > diffY = new ArrayList <Integer >();
130+ for (Cell cell : slice ){
131+ if (!diffX .contains (cell .y )){
132+ diffX .add (cell .y );
133+ }
134+ }
135+ return diffY ;
136+ }
137+
138+ private int findMinY (List <Cell > slice ){
139+ Integer minYCoord = null ;
140+ for (Cell cell : slice ){
97141 if (minYCoord != null ){
98142 if (cell .y < minYCoord ){
99143 minYCoord = cell .y ;
@@ -105,12 +149,9 @@ private int findMinYAndFillAllDifferenceX(List<Cell> slice, List<Integer> diffX)
105149 return minYCoord ;
106150 }
107151
108- private int findMaxYAndFillAllDifferenceX (List <Cell > slice , List < Integer > diffX ){
152+ private int findMaxY (List <Cell > slice ){
109153 Integer maxYCoord = null ;
110154 for (Cell cell : slice ){
111- if (!diffX .contains (cell .x )){
112- diffX .add (cell .x );
113- }
114155 if (maxYCoord != null ){
115156 if (cell .y > minYCoord ){
116157 maxYCoord = cell .y ;
@@ -122,12 +163,9 @@ private int findMaxYAndFillAllDifferenceX(List<Cell> slice, List<Integer> diffX)
122163 return maxYCoord ;
123164 }
124165
125- private int findMinXAndFillAllDifferenceY (List <Cell > slice , List < Integer > diffY ){
166+ private int findMinX (List <Cell > slice ){
126167 Integer minXCoord = null ;
127168 for (Cell cell : slice ){
128- if (!diffY .contains (cell .y )){
129- diffY .add (cell .y );
130- }
131169 if (minXCoord != null ){
132170 if (cell .x < minXCoord ){
133171 minXCoord = cell .x ;
@@ -139,12 +177,9 @@ private int findMinXAndFillAllDifferenceY(List<Cell> slice, List<Integer> diffY)
139177 return minXCoord ;
140178 }
141179
142- private int findMaxXAndFillAllDifferenceY (List <Cell > slice , List < Integer > diffY ){
180+ private int findMaxX (List <Cell > slice ){
143181 Integer maxXCoord = null ;
144182 for (Cell cell : slice ){
145- if (!diffY .contains (cell .y )){
146- diffY .add (cell .y );
147- }
148183 if (maxXCoord != null ){
149184 if (cell .x > minXCoord ){
150185 maxXCoord = cell .x ;
@@ -175,4 +210,12 @@ private boolean didPizzaContainsAllNeededVerticalCells(List<Cell> pizza, List<In
175210 }
176211 return returnValue ;
177212 }
213+
214+ private List <Integer > fillArrayWithIntValues (int start , int end ){
215+ List <Integer > returnedList = new ArrayList <Integer >();
216+ for (int i = start ; i < end + 1 ; i ++){
217+ returnedList .add ((Integer )i );
218+ }
219+ return returnedList ;
220+ }
178221}
0 commit comments