-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
162 lines (120 loc) · 3.95 KB
/
Solution.java
File metadata and controls
162 lines (120 loc) · 3.95 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
package hackrank.algorithm.search.luck;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* Count Luck Challenge
*
* @see https://www.hackerrank.com/challenges/count-luck
*/
public class Solution {
public static void main(String[] args) {
for (Grid grid : readInput(System.in)) {
System.out.println((grid.countOptimalChoices() == grid.numberChoices) ? "Impressed" : "Oops!");
}
}
public static List<Grid> readInput(InputStream stream) {
Scanner scanner = new Scanner(stream);
int numberTests = scanner.nextInt();
List<Grid> grids = new ArrayList<>();
for (int t = 0; t < numberTests; t++) {
int rows = scanner.nextInt();
int columns = scanner.nextInt();
char[][] data = new char[rows][columns];
for (int r = 0; r < rows; r++) {
String row = scanner.next();
for (int c = 0; c < row.length(); c++) {
data[r][c] = row.charAt(c);
}
}
int numberChoices = scanner.nextInt();
grids.add(new Grid(data, numberChoices));
}
scanner.close();
return grids;
}
}
class Grid {
enum CellType {
HERMIONE('M'), EXIT('*'), EMPTY('.'), TREE('X');
private char value;
CellType(char value) {
this.value = value;
}
char getValue() {
return value;
}
}
char[][] data;
int numberChoices;
int startRow;
int startColumn;
Grid(char[][] data, int numberLuckyChoices) {
this.data = data;
this.numberChoices = numberLuckyChoices;
for (int r = 0; r < getNumberRows(); r++) {
for (int c = 0; c < getNumberColumns(); c++) {
if (data[r][c] == CellType.HERMIONE.getValue()) {
startRow = r;
startColumn = c;
}
}
}
}
public int countOptimalChoices() {
Set<String> visisted = new HashSet<>();
return depthFirstSearch(startRow, startColumn, visisted, 0);
}
private int depthFirstSearch(int row, int column, Set<String> visited, int optionsFound) {
char value = data[row][column];
visited.add(row + "-" + column);
if (value == CellType.EXIT.getValue()) {
return optionsFound;
}
List<Integer[]> adjacent = adjacentAvailable(row, column, visited);
if (adjacent.isEmpty()) {
return -1;
}
if (adjacent.size() > 1) {
optionsFound++;
}
for (Integer[] cell : adjacent) {
int found = depthFirstSearch(cell[0], cell[1], visited, optionsFound);
if (found != -1) {
return found;
}
}
return -1;
}
private List<Integer[]> adjacentAvailable(int row, int column, Set<String> visited) {
int[][] directions = { { row - 1, column }, { row, column - 1 }, { row, column + 1 }, { row + 1, column } };
List<Integer[]> available = new ArrayList<>();
for (int[] cell : directions) {
int r = cell[0];
int c = cell[1];
if (isAvailable(r, c) && !visited.contains(r + "-" + c)) {
available.add(new Integer[] { r, c });
}
}
return available;
}
public boolean isAvailable(int row, int column) {
if (row < 0 || row >= getNumberRows()) {
return false;
}
if (column < 0 || column >= getNumberColumns()) {
return false;
}
char value = data[row][column];
return value == CellType.EMPTY.getValue() || value == CellType.EXIT.getValue();
}
public int getNumberRows() {
return data.length;
}
public int getNumberColumns() {
return data[0].length;
}
}