Skip to content

Commit aaa4d9d

Browse files
committed
added methods for DFS (steps: up + left + down + right)
1 parent 7fa8135 commit aaa4d9d

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>GoogleHashCode2017</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.google.hashcode;
2+
3+
public class DFSMethods {
4+
5+
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>();
8+
// finding min y coordinate in slice and fill values to differenceXCoordinates.
9+
Integer minYCoordInSlice = findMinYAndFillAllDifferenceX(slice, differenceXCoordinates);
10+
11+
//prevent this method for cutting too big slice.
12+
if(slice.size() + differenceXCoordinates.size() > maxCellsInSlice) return false;
13+
14+
//our pizza still have all needed cells for this step? lets check it.
15+
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededHorizontalCells(pizza,differenceXCoordinates, minYCoordInSlice -1);
16+
17+
if(pizzaContainsAllNeededCells){
18+
//Cut all needed cells from pizza and add their to our slice.
19+
for(int xCoord: differenceXCoordinates){
20+
//use .remove(index) to get object from pizza (with it's Ingradient)
21+
//but for getting those index use .indexOf(Object o)
22+
slice.add(pizza.remove(pizza.indexOf(new Cell(xCoord, minYCoordInSlice))))
23+
}
24+
return true;
25+
}else{
26+
//oups, pizza doesn't contain one of needed cells, step blocked.
27+
return false;
28+
}
29+
}
30+
31+
public boolean stepRight(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice){
32+
33+
List<Integer> differenceYCoordinates = new ArrayList<Integer>();
34+
Integer maxXCoordInSlice = findMaxXAndFillAllDifferenceY(slice, differenceYCoordinates);
35+
36+
if(slice.size() + differenceYCoordinates.size() > maxCellsInSlice) return false;
37+
38+
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, maxXCoordInSlice + 1);
39+
40+
if(pizzaContainsAllNeededCells){
41+
for(int yCoord: differenceYCoordinates){
42+
slice.add(pizza.remove(pizza.indexOf(new Cell(minXCoordInSlice, yCoord))))
43+
}
44+
return true;
45+
}else{
46+
return false;
47+
}
48+
49+
}
50+
51+
public boolean stepDown(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice){
52+
53+
List<Integer> differenceXCoordinates = new ArrayList<Integer>();
54+
Integer maxYCoordInSlice = findMaxYAndFillAllDifferenceX(slice, differenceXCoordinates);
55+
56+
if(slice.size() + differenceXCoordinates.size() > maxCellsInSlice) return false;
57+
58+
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza,differenceXCoordinates, maxYCoordInSlice + 1);
59+
60+
if(pizzaContainsAllNeededCells){
61+
for(int xCoord: differenceXCoordinates){
62+
slice.add(pizza.remove(pizza.indexOf(new Cell(xCoord, maxYCoordInSlice))))
63+
}
64+
return true;
65+
}else{
66+
return false;
67+
}
68+
69+
}
70+
71+
public boolean stepLeft(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice){
72+
73+
List<Integer> differenceYCoordinates = new ArrayList<Integer>();
74+
Integer minXCoordInSlice = findMinXAndFillAllDifferenceY(slice, differenceYCoordinates);
75+
76+
if(slice.size() + differenceYCoordinates.size() > maxCellsInSlice) return false;
77+
78+
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, minXCoordInSlice -1 1);
79+
80+
if(pizzaContainsAllNeededCells){
81+
for(int yCoord: differenceYCoordinates){
82+
slice.add(pizza.remove(pizza.indexOf(new Cell(minXCoordInSlice, yCoord))))
83+
}
84+
return true;
85+
}else{
86+
return false;
87+
}
88+
89+
}
90+
91+
private int findMinYAndFillAllDifferenceX(List<Cell> slice, List<Integer> diffX){
92+
Integer minYCoord = null;
93+
for(Cell cell: slice){
94+
if(!diffX.contains(cell.x)){
95+
diffX.add(cell.x);
96+
}
97+
if(minYCoord != null){
98+
if(cell.y < minYCoord){
99+
minYCoord = cell.y;
100+
}
101+
}else{
102+
minYCoord = cell.y;
103+
}
104+
}
105+
return minYCoord;
106+
}
107+
108+
private int findMaxYAndFillAllDifferenceX(List<Cell> slice, List<Integer> diffX){
109+
Integer maxYCoord = null;
110+
for(Cell cell: slice){
111+
if(!diffX.contains(cell.x)){
112+
diffX.add(cell.x);
113+
}
114+
if(maxYCoord != null){
115+
if(cell.y > minYCoord){
116+
maxYCoord = cell.y;
117+
}
118+
}else{
119+
maxYCoord = cell.y;
120+
}
121+
}
122+
return maxYCoord;
123+
}
124+
125+
private int findMinXAndFillAllDifferenceY(List<Cell> slice, List<Integer> diffY){
126+
Integer minXCoord = null;
127+
for(Cell cell: slice){
128+
if(!diffY.contains(cell.y)){
129+
diffY.add(cell.y);
130+
}
131+
if(minXCoord != null){
132+
if(cell.x < minXCoord){
133+
minXCoord = cell.x;
134+
}
135+
}else{
136+
minXCoord = cell.x;
137+
}
138+
}
139+
return minXCoord;
140+
}
141+
142+
private int findMaxXAndFillAllDifferenceY(List<Cell> slice, List<Integer> diffY){
143+
Integer maxXCoord = null;
144+
for(Cell cell: slice){
145+
if(!diffY.contains(cell.y)){
146+
diffY.add(cell.y);
147+
}
148+
if(maxXCoord != null){
149+
if(cell.x > minXCoord){
150+
maxXCoord = cell.x;
151+
}
152+
}else{
153+
maxXCoord = cell.x;
154+
}
155+
}
156+
return maxXCoord;
157+
}
158+
159+
private boolean didPizzaContainsAllNeededHorizontalCells(List<Cell> pizza, List<Integer> differenceXCoordinate, Integer yCoord){
160+
boolean returnValue = true;
161+
for(Integer xCoord: differenceXCoordinate){
162+
if(!pizza.contains(new Cell(xCoord, yCoord))){
163+
returnValue = false;
164+
}
165+
}
166+
return returnValue;
167+
}
168+
169+
private boolean didPizzaContainsAllNeededVerticalCells(List<Cell> pizza, List<Integer> differenceYCoordinate, Integer minXCoordinate){
170+
boolean returnValue = true;
171+
for(Integer yCoord: differenceYCoordinate){
172+
if(!pizza.contains(new Cell(minXCoordinate, yCoord))){
173+
returnValue = false;
174+
}
175+
}
176+
return returnValue;
177+
}
178+
}

0 commit comments

Comments
 (0)